Create Address Book in Python – Using Tkinter

28 Apr, 2021
Prerequisite: Tkinter
In this article, we will discuss how to create an address book in Tkinter using Python.
Step 1: Creating GUI.
In this, we will add all the GUI Components like labels, text area and buttons.
Python3
# Import Modulefrom tkinter import *  # Create Objectroot = Tk()  # Set geometryroot.geometry('400x500')  # Add Buttons, Label, ListBoxName = StringVar()Number = StringVar()  frame = Frame()frame.pack(pady=10)  frame1 = Frame()frame1.pack()  frame2 = Frame()frame2.pack(pady=10)  Label(frame, text = 'Name', font='arial 12 bold').pack(side=LEFT)Entry(frame, textvariable = Name,width=50).pack()  Label(frame1, text = 'Phone No.', font='arial 12 bold').pack(side=LEFT)Entry(frame1, textvariable = Number,width=50).pack()  Label(frame2, text = 'Address', font='arial 12 bold').pack(side=LEFT)address = Text(frame2,width=37,height=10)address.pack()  Button(root,text="Add",font="arial 12 bold").place(x= 100, y=270)Button(root,text="View",font="arial 12 bold").place(x= 100, y=310)Button(root,text="Delete",font="arial 12 bold").place(x= 100, y=350)Button(root,text="Reset",font="arial 12 bold").place(x= 100, y=390)  scroll_bar = Scrollbar(root, orient=VERTICAL)select = Listbox(root, yscrollcommand=scroll_bar.set, height=12)scroll_bar.config (command=select.yview)scroll_bar.pack(side=RIGHT, fill=Y)select.place(x=200,y=260)  # Execute Tkinterroot.mainloop()
Output:
Step 2: Creating User define function to retrieve the operation. 
These are function are used in this program:
add: This will add a record in the address book data structure and update the GUI.
view: This will represent all the values of the selected record.
delete: This will delete the selected record from the address book data structure and update the GUI.
reset: This will reset all the input values of the input parameters.
update_book: This will update the whole address book data structure.
Python3
# Information Listdatas = []  # Add Informationdef add():    global datas    datas.append([Name.get(),Number.get(),address.get(1.0, "end-1c")])    update_book()  # View Informationdef view():    Name.set(datas[int(select.curselection()[0])][0])    Number.set(datas[int(select.curselection()[0])][1])    address.delete(1.0,"end")    address.insert(1.0, datas[int(select.curselection()[0])][2])  # Delete Informationdef delete():    del datas[int(select.curselection()[0])]    update_book()  def reset():    Name.set('')    Number.set('')    address.delete(1.0,"end")  # Update Informationdef update_book():    select.delete(0,END)          for n,p,a in datas:        select.insert(END, n)
Complete Code:
Python3
# Import Modulefrom tkinter import *  # Create Objectroot = Tk()  # Set geometryroot.geometry('400x500')  # Information Listdatas = []  # Add Informationdef add():    global datas    datas.append([Name.get(),Number.get(),address.get(1.0, "end-1c")])    update_book()  # View Informationdef view():    Name.set(datas[int(select.curselection()[0])][0])    Number.set(datas[int(select.curselection()[0])][1])    address.delete(1.0,"end")    address.insert(1.0, datas[int(select.curselection()[0])][2])  # Delete Informationdef delete():    del datas[int(select.curselection()[0])]    update_book()  def reset():    Name.set('')    Number.set('')    address.delete(1.0,"end")  # Update Informationdef update_book():    select.delete(0,END)    for n,p,a in datas:        select.insert(END, n)  # Add Buttons, Label, ListBoxName = StringVar()Number = StringVar()  frame = Frame()frame.pack(pady=10)  frame1 = Frame()frame1.pack()  frame2 = Frame()frame2.pack(pady=10)  Label(frame, text = 'Name', font='arial 12 bold').pack(side=LEFT)Entry(frame, textvariable = Name,width=50).pack()  Label(frame1, text = 'Phone No.', font='arial 12 bold').pack(side=LEFT)Entry(frame1, textvariable = Number,width=50).pack()  Label(frame2, text = 'Address', font='arial 12 bold').pack(side=LEFT)address = Text(frame2,width=37,height=10)address.pack()  Button(root,text="Add",font="arial 12 bold",command=add).place(x= 100, y=270)Button(root,text="View",font="arial 12 bold",command=view).place(x= 100, y=310)Button(root,text="Delete",font="arial 12 bold",command=delete).place(x= 100, y=350)Button(root,text="Reset",font="arial 12 bold",command=reset).place(x= 100, y=390)  scroll_bar = Scrollbar(root, orient=VERTICAL)select = Listbox(root, yscrollcommand=scroll_bar.set, height=12)scroll_bar.config (command=select.yview)scroll_bar.pack(side=RIGHT, fill=Y)select.place(x=200,y=260)  # Execute Tkinterroot.mainloop()
Output:
Python Tkinter-exercises
Python Tkinter-projects
Python-tkinter
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.