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

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 first part.

There are six different sections and I separated them into six different python files. Sections are : Home, My People, About Us, Add People, Update People, Display People and there is also a feature to delete peoples.

Let us jump to the codes of the Homepage. I named the file "main.py".

Importing

import tkinter as tk
import _sqlite3
from tkinter import PhotoImage, messagebox
import datetime
from add_people import Add_people
from update_people import Update_people
from display import Display_people

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

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



I've added the other files in the import section but not posting those files now because the blog will be large. After posting all the parts you can run the codes.

Class and Functions


class my_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='My 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)

        # ------------------- list box and scrollbar ----------------

        self.scroll = tk.Scrollbar(self.bottom, orient=tk.VERTICAL)

        self.listbox = tk.Listbox(self.bottom, width=60, height=24)
        self.listbox.grid(row=0, column=0, padx=(40,0))
        self.scroll.config(command = self.listbox.yview)
        self.listbox.config(yscrollcommand=self.scroll.set)

        persons = cur.execute('select * from "addressbook"').fetchall()

        count = 0
        for person in persons:
            self.listbox.insert(count, str(person[0])+'.  '+person[1]+' '+person[2])
            count+=1

        self.scroll.grid(row=0, column=1, sticky=tk.N + tk.S)
        # btn

        btn_add = tk.Button(self.bottom, text='Add', width=12, font='Sans 12 bold', command=self.add_people)
        btn_add.grid(row=0, column=2, padx=20, pady=10, sticky=tk.N)

        btn_update = tk.Button(self.bottom, text='Update', width=12, font='Sans 12 bold', command = self.update_people)
        btn_update.grid(row=0, column=2, padx=20, pady=50, sticky=tk.N)

        btn_display = tk.Button(self.bottom, text='Display', width=12, font='Sans 12 bold', command = self.display_people)
        btn_display.grid(row=0, column=2, padx=20, pady=90, sticky=tk.N)

        btn_delete = tk.Button(self.bottom, text='Delete', width=12, font='Sans 12 bold', command = self.delete_people)
        btn_delete.grid(row=0, column=2, padx=20, pady=130, sticky=tk.N)

    def add_people(self):
        add_page = Add_people()
        self.destroy()

    def update_people(self):
        try:
            selected_item = self.listbox.curselection()
            person = self.listbox.get(selected_item)
            person_id = person.split('.')[0]
            updatepeople = Update_people(person_id)
            self.destroy()
        except:
            tk.messagebox.showinfo('Error', 'Select an item first.')
            self.destroy()

    def display_people(self):
        selected_item = self.listbox.curselection()
        person = self.listbox.get(selected_item)
        person_id = person.split('.')[0]
        displaypeople = Display_people(person_id)
        self.destroy()

    def delete_people(self):
        selected_item = self.listbox.curselection()
        person = self.listbox.get(selected_item)
        person_id = person.split('.')[0]

        query = "delete from 'addressbook' where person_id = {}".format(person_id)

        person_name = person.split('.')[1]
        answer = tk.messagebox.askquestion('Warning', f'Do you really want to delete {person_name} ?')
        if answer:
            try:
                cur.execute(query)
                con.commit()
                tk.messagebox.showinfo('Success', 'Successfully Deleted!')
            except Exception as e:
                tk.messagebox.showinfo('Error', str(e))

        else:
            pass

        self.destroy()


The full main.py file's code is:-

import tkinter as tk
import _sqlite3
from tkinter import PhotoImage, messagebox
import datetime
from add_people import Add_people
from update_people import Update_people
from display import Display_people

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

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


class my_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='My 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)

        # ------------------- list box and scrollbar ----------------

        self.scroll = tk.Scrollbar(self.bottom, orient=tk.VERTICAL)

        self.listbox = tk.Listbox(self.bottom, width=60, height=24)
        self.listbox.grid(row=0, column=0, padx=(40,0))
        self.scroll.config(command = self.listbox.yview)
        self.listbox.config(yscrollcommand=self.scroll.set)

        persons = cur.execute('select * from "addressbook"').fetchall()

        count = 0
        for person in persons:
            self.listbox.insert(count, str(person[0])+'.  '+person[1]+' '+person[2])
            count+=1

        self.scroll.grid(row=0, column=1, sticky=tk.N + tk.S)
        # btn

        btn_add = tk.Button(self.bottom, text='Add', width=12, font='Sans 12 bold', command=self.add_people)
        btn_add.grid(row=0, column=2, padx=20, pady=10, sticky=tk.N)

        btn_update = tk.Button(self.bottom, text='Update', width=12, font='Sans 12 bold', command = self.update_people)
        btn_update.grid(row=0, column=2, padx=20, pady=50, sticky=tk.N)

        btn_display = tk.Button(self.bottom, text='Display', width=12, font='Sans 12 bold', command = self.display_people)
        btn_display.grid(row=0, column=2, padx=20, pady=90, sticky=tk.N)

        btn_delete = tk.Button(self.bottom, text='Delete', width=12, font='Sans 12 bold', command = self.delete_people)
        btn_delete.grid(row=0, column=2, padx=20, pady=130, sticky=tk.N)

    def add_people(self):
        add_page = Add_people()
        self.destroy()

    def update_people(self):
        try:
            selected_item = self.listbox.curselection()
            person = self.listbox.get(selected_item)
            person_id = person.split('.')[0]
            updatepeople = Update_people(person_id)
            self.destroy()
        except:
            tk.messagebox.showinfo('Error', 'Select an item first.')
            self.destroy()

    def display_people(self):
        selected_item = self.listbox.curselection()
        person = self.listbox.get(selected_item)
        person_id = person.split('.')[0]
        displaypeople = Display_people(person_id)
        self.destroy()

    def delete_people(self):
        selected_item = self.listbox.curselection()
        person = self.listbox.get(selected_item)
        person_id = person.split('.')[0]

        query = "delete from 'addressbook' where person_id = {}".format(person_id)

        person_name = person.split('.')[1]
        answer = tk.messagebox.askquestion('Warning', f'Do you really want to delete {person_name} ?')
        if answer:
            try:
                cur.execute(query)
                con.commit()
                tk.messagebox.showinfo('Success', 'Successfully Deleted!')
            except Exception as e:
                tk.messagebox.showinfo('Error', str(e))

        else:
            pass

        self.destroy()

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center