Phonebook GUI Application Made With Python and Tkinter (Part III).

in #programming3 years ago

I am a newbie on the programming world and I am trying to learn Python as it is the easiest and very strong programming language. I've learnt all basic theories and the programming syntax and then I have solved some problems from urionlinejudge. Now I'am trying to learn GUI Appliations and I am trying to learn Tkinter module of Python.

Previously I posted a Restaurant Management System GUI application that I made with Python and Tkinter module of python. Here is Phonebook GUI Application Made With Python and Tkinter. I will post the code into different parts. This is the third part.

First Part

Second Part

Let us jump to the codes of "add_people.py".


Importing

import tkinter as tk
import _sqlite3
from tkinter import PhotoImage
import datetime
from tkinter import messagebox

date = datetime.datetime.now().date()
date = str(date)

con = _sqlite3.connect('database.db')
cur = con.cursor()


Class "Add_people"

class Add_people(tk.Toplevel):
    def __init__(self):
        tk.Toplevel.__init__(self)

        self.geometry('650x550+380+80')
        self.title('My People')
        self.resizable(0,0)

        self.top = tk.Frame(self, height=150, bg='white')
        self.top.pack(fill=tk.X)

        self.bottom = tk.Frame(self, height=500, bg='#ebb134')
        self.bottom.pack(fill=tk.X)

        # ---------- icon ------------
        self.top_image = PhotoImage(file='icons/people.png')
        self.top_image_label = tk.Label(self.top, image=self.top_image, bg='white')
        self.top_image_label.place(x=130, y=25)

        # -------------------- heading ----------------

        self.heading = tk.Label(self.top, text='Add New People', bg='white', font='arial 15 bold', fg='#34baeb')
        self.heading.place(x=230, y=40)

        # ------------------------ date -------------------------------

        self.date_lbl = tk.Label(self.top, text=f'Date: {date}', font='arial 11 bold', bg='white', fg='#ebb434')
        self.date_lbl.place(x=500, y=10)

        # name
        self.label_name = tk.Label(self.bottom, text='Name: ', font='Arial 12 bold', bg='#ebb134')
        self.label_name.place(x=40, y=40)

        self.entry_name = tk.Entry(self.bottom, width=30, bd=4)
        self.entry_name.place(x=175, y=40)

        # surname

        self.label_surname = tk.Label(self.bottom, text='Surname: ', font='Arial 12 bold', bg='#ebb134')
        self.label_surname.place(x=40, y=80)

        self.entry_surname = tk.Entry(self.bottom, width=30, bd=4)
        self.entry_surname.place(x=175, y=80)

        # email

        self.label_email = tk.Label(self.bottom, text='Email: ', font='Arial 12 bold', bg='#ebb134')
        self.label_email.place(x=40, y=120)

        self.entry_email = tk.Entry(self.bottom, width=30, bd=4)
        self.entry_email.place(x=175, y=120)

        # phone number

        self.label_phone = tk.Label(self.bottom, text='Phone Number: ', font='Arial 12 bold', bg='#ebb134')
        self.label_phone.place(x=40, y=160)

        self.entry_phone = tk.Entry(self.bottom, width=30, bd=4)
        self.entry_phone.place(x=175, y=160)

        # address

        self.label_Address = tk.Label(self.bottom, text='Address: ', font='Arial 12 bold', bg='#ebb134')
        self.label_Address.place(x=40, y=200)

        self.entry_Address = tk.Text(self.bottom, width=24,height=3, wrap='word')
        self.entry_Address.place(x=175, y=200)

        # submit btn

        self.submit_btn = tk.Button(self.bottom, text='Submit', font='Arial 12 bold', command = self.add_people)
        self.submit_btn.place(x=130, y=280)


Functions inside "Add_people" class

    def add_people(self):
        name = self.entry_name.get()
        surname = self.entry_surname.get()
        email = self.entry_email.get()
        phone = self.entry_phone.get()
        address = self.entry_Address.get(1.0,'end-1c')


Condition and exception handling inside "add_people" function

        if name and surname and email and phone and address != '':
            try:
                query = 'insert into "addressbook" (person_name, person_surname, person_email, person_phone, person_address) values(?,?,?,?,?)'
                cur.execute(query,(name, surname, email, phone, address))
                con.commit()
                tk.messagebox.showinfo('Success', 'Contact added', icon='info')

            except Exception as e:
                tk.messagebox.showerror('Error', str(e), icon='warning')
        else:
            tk.messagebox.showerror('Error', 'Fill all the fields', icon='warning')

        self.destroy()


So the full codes of "add_people.py" is :-

import tkinter as tk
import _sqlite3
from tkinter import PhotoImage
import datetime
from tkinter import messagebox

date = datetime.datetime.now().date()
date = str(date)

con = _sqlite3.connect('database.db')
cur = con.cursor()


class Add_people(tk.Toplevel):
    def __init__(self):
        tk.Toplevel.__init__(self)

        self.geometry('650x550+380+80')
        self.title('My People')
        self.resizable(0,0)

        self.top = tk.Frame(self, height=150, bg='white')
        self.top.pack(fill=tk.X)

        self.bottom = tk.Frame(self, height=500, bg='#ebb134')
        self.bottom.pack(fill=tk.X)

        # ---------- icon ------------
        self.top_image = PhotoImage(file='icons/people.png')
        self.top_image_label = tk.Label(self.top, image=self.top_image, bg='white')
        self.top_image_label.place(x=130, y=25)

        # -------------------- heading ----------------

        self.heading = tk.Label(self.top, text='Add New People', bg='white', font='arial 15 bold', fg='#34baeb')
        self.heading.place(x=230, y=40)

        # ------------------------ date -------------------------------

        self.date_lbl = tk.Label(self.top, text=f'Date: {date}', font='arial 11 bold', bg='white', fg='#ebb434')
        self.date_lbl.place(x=500, y=10)

        # name
        self.label_name = tk.Label(self.bottom, text='Name: ', font='Arial 12 bold', bg='#ebb134')
        self.label_name.place(x=40, y=40)

        self.entry_name = tk.Entry(self.bottom, width=30, bd=4)
        self.entry_name.place(x=175, y=40)

        # surname

        self.label_surname = tk.Label(self.bottom, text='Surname: ', font='Arial 12 bold', bg='#ebb134')
        self.label_surname.place(x=40, y=80)

        self.entry_surname = tk.Entry(self.bottom, width=30, bd=4)
        self.entry_surname.place(x=175, y=80)

        # email

        self.label_email = tk.Label(self.bottom, text='Email: ', font='Arial 12 bold', bg='#ebb134')
        self.label_email.place(x=40, y=120)

        self.entry_email = tk.Entry(self.bottom, width=30, bd=4)
        self.entry_email.place(x=175, y=120)

        # phone number

        self.label_phone = tk.Label(self.bottom, text='Phone Number: ', font='Arial 12 bold', bg='#ebb134')
        self.label_phone.place(x=40, y=160)

        self.entry_phone = tk.Entry(self.bottom, width=30, bd=4)
        self.entry_phone.place(x=175, y=160)

        # address

        self.label_Address = tk.Label(self.bottom, text='Address: ', font='Arial 12 bold', bg='#ebb134')
        self.label_Address.place(x=40, y=200)

        self.entry_Address = tk.Text(self.bottom, width=24,height=3, wrap='word')
        self.entry_Address.place(x=175, y=200)

        # submit btn

        self.submit_btn = tk.Button(self.bottom, text='Submit', font='Arial 12 bold', command = self.add_people)
        self.submit_btn.place(x=130, y=280)

    def add_people(self):
        name = self.entry_name.get()
        surname = self.entry_surname.get()
        email = self.entry_email.get()
        phone = self.entry_phone.get()
        address = self.entry_Address.get(1.0,'end-1c')

        if name and surname and email and phone and address != '':
            try:
                query = 'insert into "addressbook" (person_name, person_surname, person_email, person_phone, person_address) values(?,?,?,?,?)'
                cur.execute(query,(name, surname, email, phone, address))
                con.commit()
                tk.messagebox.showinfo('Success', 'Contact added', icon='info')

            except Exception as e:
                tk.messagebox.showerror('Error', str(e), icon='warning')
        else:
            tk.messagebox.showerror('Error', 'Fill all the fields', icon='warning')

        self.destroy()
Sort:  

Congratulations @here-to-share! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You received more than 800 upvotes.
Your next target is to reach 900 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Happy New Year - Feedback from the first Hive Power Up Day of 2022
Support the HiveBuzz project. Vote for our proposal!