· 7 years ago · Jan 10, 2019, 05:04 AM
1from tkinter import *
2import os
3import sqlite3
4import tkinter.ttk as ttk
5import tkinter.messagebox as tkMessageBox
6import os.path
7from os import path
8import webbrowser as wb
9
10root = Tk()
11root.title("Enter Names")
12screen_width = root.winfo_screenwidth()
13screen_height = root.winfo_screenheight()
14width = 1100
15height = 400
16x = (screen_width/2) - (width/2)
17y = (screen_height/2) - (height/2)
18root.geometry('%dx%d+%d+%d' % (width, height, x, y))
19root.resizable(0, 0)
20
21#==================================METHODS============================================
22def Database():
23 global conn, cursor
24 conn = sqlite3.connect('contactlist.db')
25 cursor = conn.cursor()
26 cursor.execute("CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, firstname TEXT, lastname TEXT, gender TEXT, address TEXT, phone TEXT)")
27
28def Create():
29 if FIRSTNAME.get() == "" or " " in FIRSTNAME.get() or LASTNAME.get() == "" or " " in LASTNAME.get() or GENDER.get() == "" or ADDRESS.get() == "" or PHONE.get() == "":
30 txt_result.config(text="Please complete the required field!", fg="red")
31 else:
32 Database()
33 cursor.execute("INSERT INTO `member` (firstname, lastname, gender, address, phone) VALUES(?, ?, ?, ?, ?)", (str(FIRSTNAME.get()), str(LASTNAME.get()), str(GENDER.get()), str(ADDRESS.get()), str(PHONE.get())))
34 conn.commit()
35 FIRSTNAME.set("")
36 LASTNAME.set("")
37 GENDER.set("")
38 ADDRESS.set("")
39 PHONE.set("")
40 cursor.close()
41 conn.close()
42 txt_result.config(text="Created contact!", fg="green")
43 Read()
44
45def Read():
46 tree.delete(*tree.get_children())
47 Database()
48 cursor.execute("SELECT * FROM `member` ORDER BY `lastname` ASC")
49 fetch = cursor.fetchall()
50 for data in fetch:
51 tree.insert('', 'end', values=(data[1], data[2], data[3], data[4], data[5]))
52 cursor.close()
53 conn.close()
54 txt_result.config(text="Successfully read the data from database", fg="black")
55
56def Clear():
57 result = tkMessageBox.askquestion('Name Entry: By Kainoa Kanter', 'Are you sure you want to clear all contacts?', icon="warning")
58 if result == 'yes':
59 os.remove('contactlist.db')
60 txt_result.config(text="Cleared all contacts!", fg="red")
61 Read()
62
63def Exit():
64 result = tkMessageBox.askquestion('Name Entry: By Kainoa Kanter', 'Are you sure you want to exit?', icon="warning")
65 if result == 'yes':
66 root.destroy()
67 exit()
68
69def Maps():
70 global conn, cursor
71 conn = sqlite3.connect('contactlist.db')
72 cursor = conn.cursor()
73 cursor.execute("SELECT * FROM `member` ORDER BY `lastname` ASC")
74 fetch = cursor.fetchall()
75 for data in fetch:
76 x=data[4]
77 open_string = "https://www.google.com/maps/place/{}"
78 wb.open_new_tab(open_string.format(x))
79
80#==================================VARIABLES==========================================
81FIRSTNAME = StringVar()
82LASTNAME = StringVar()
83GENDER = StringVar()
84ADDRESS = StringVar()
85PHONE = StringVar()
86
87#==================================FRAME==============================================
88Top = Frame(root, width=1100, height=40, bd=8, relief="raise")
89Top.pack(side=TOP)
90Left = Frame(root, width=300, height=500, bd=8, relief="raise")
91Left.pack(side=LEFT)
92Right = Frame(root, width=600, height=500, bd=8, relief="raise")
93Right.pack(side=RIGHT)
94Forms = Frame(Left, width=300, height=450)
95Forms.pack(side=TOP)
96Buttons = Frame(Left, width=300, height=100, bd=8, relief="raise")
97Buttons.pack(side=BOTTOM)
98RadioGroup = Frame(Forms)
99Male = Radiobutton(RadioGroup, text="Male", variable=GENDER, value="Male", font=('Terminal', 16)).pack(side=LEFT)
100Female = Radiobutton(RadioGroup, text="Female", variable=GENDER, value="Female", font=('Terminal', 16)).pack(side=LEFT)
101
102#==================================LABEL WIDGET=======================================
103txt_title = Label(Top, width=900, font=('Terminal', 24), text = "Kainoa Kanter's Contact Book")
104txt_title.pack()
105txt_firstname = Label(Forms, text="First name:", font=('Terminal', 16), bd=15)
106txt_firstname.grid(row=0, stick="e")
107txt_lastname = Label(Forms, text="Last name:", font=('Terminal', 16), bd=15)
108txt_lastname.grid(row=1, stick="e")
109txt_gender = Label(Forms, text="Gender:", font=('Terminal', 16), bd=15)
110txt_gender.grid(row=2, stick="e")
111txt_address = Label(Forms, text="Address:", font=('Terminal', 16), bd=15)
112txt_address.grid(row=3, stick="e")
113txt_phone = Label(Forms, text="Phone:", font=('Terminal', 16), bd=15)
114txt_phone.grid(row=4, stick="e")
115txt_result = Label(Buttons)
116txt_result.pack(side=TOP)
117
118#==================================ENTRY WIDGET=======================================
119firstname = Entry(Forms, textvariable=FIRSTNAME, width=30)
120firstname.grid(row=0, column=1)
121lastname = Entry(Forms, textvariable=LASTNAME, width=30)
122lastname.grid(row=1, column=1)
123RadioGroup.grid(row=2, column=1)
124address = Entry(Forms, textvariable=ADDRESS, width=30)
125address.grid(row=3, column=1)
126phone = Entry(Forms, textvariable=PHONE, width=30)
127phone.grid(row=4, column=1)
128
129#==================================BUTTONS WIDGET=====================================
130btn_create = Button(Buttons, width=10, text="Create", command=Create)
131btn_create.pack(side=LEFT)
132btn_read = Button(Buttons, width=10, text="Read", command=Read )
133btn_read.pack(side=LEFT)
134btn_delete = Button(Buttons, width=10, text="Clear", command=Clear)
135btn_delete.pack(side=LEFT)
136btn_maps = Button(Buttons, width=10, text="Maps", command=Maps)
137btn_maps.pack(side=LEFT)
138btn_exit = Button(Buttons, width=10, text="Exit", command=Exit)
139btn_exit.pack(side=LEFT)
140
141#==================================LIST WIDGET========================================
142scrollbary = Scrollbar(Right, orient=VERTICAL)
143scrollbarx = Scrollbar(Right, orient=HORIZONTAL)
144tree = ttk.Treeview(Right, columns=("First name", "Last name", "Gender", "Address", "Phone"), selectmode="extended", height=500, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
145scrollbary.config(command=tree.yview)
146scrollbary.pack(side=RIGHT, fill=Y)
147scrollbarx.config(command=tree.xview)
148scrollbarx.pack(side=BOTTOM, fill=X)
149tree.heading('First name', text="First name", anchor=W)
150tree.heading('Last name', text="Last name", anchor=W)
151tree.heading('Gender', text="Gender", anchor=W)
152tree.heading('Address', text="Address", anchor=W)
153tree.heading('Phone', text="Phone", anchor=W)
154tree.column('#0', stretch=NO, minwidth=0, width=0)
155tree.column('#1', stretch=NO, minwidth=0, width=80)
156tree.column('#2', stretch=NO, minwidth=0, width=120)
157tree.column('#3', stretch=NO, minwidth=0, width=80)
158tree.column('#4', stretch=NO, minwidth=0, width=150)
159tree.column('#5', stretch=NO, minwidth=0, width=150)
160tree.pack()
161
162#==================================INITIALIZATION=====================================
163if __name__ == '__main__':
164 root.mainloop()