1
+ from tkinter import *
2
+ import sqlite3
3
+ import tkinter .ttk as ttk
4
+ import tkinter .messagebox as tkMessageBox
5
+
6
+ # DEVELOPED BY Pyiterator !,
7
+ root = Tk ()
8
+ root .title ("Contact Manager" )
9
+ width = 700
10
+ height = 400
11
+ screen_width = root .winfo_screenwidth ()
12
+ screen_height = root .winfo_screenheight ()
13
+ x = (screen_width / 2 ) - (width / 2 )
14
+ y = (screen_height / 2 ) - (height / 2 )
15
+ root .geometry ("%dx%d+%d+%d" % (width , height , x , y ))
16
+ root .resizable (0 , 0 )
17
+ root .config (bg = "#1a1a1a" )
18
+
19
+ # ============================VARIABLES===================================
20
+ FIRSTNAME = StringVar ()
21
+ LASTNAME = StringVar ()
22
+ GENDER = StringVar ()
23
+ AGE = StringVar ()
24
+ ADDRESS = StringVar ()
25
+ CONTACT = StringVar ()
26
+
27
+
28
+ # ============================METHODS=====================================
29
+
30
+ def Database ():
31
+ conn = sqlite3 .connect ("pythontut.db" )
32
+ cursor = conn .cursor ()
33
+ cursor .execute (
34
+ "CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT, gender TEXT, age TEXT, address TEXT, contact TEXT)" )
35
+ cursor .execute ("SELECT * FROM `member` ORDER BY `lastname` ASC" )
36
+ fetch = cursor .fetchall ()
37
+ for data in fetch :
38
+ tree .insert ('' , 'end' , values = (data ))
39
+ cursor .close ()
40
+ conn .close ()
41
+
42
+
43
+ def SubmitData ():
44
+ if FIRSTNAME .get () == "" or LASTNAME .get () == "" or GENDER .get () == "" or AGE .get () == "" or ADDRESS .get () == "" or CONTACT .get () == "" :
45
+ result = tkMessageBox .showwarning ('' , 'Please Complete The Required Field' , icon = "warning" )
46
+ else :
47
+ tree .delete (* tree .get_children ())
48
+ conn = sqlite3 .connect ("pythontut.db" )
49
+ cursor = conn .cursor ()
50
+ cursor .execute (
51
+ "INSERT INTO `member` (firstname, lastname, gender, age, address, contact) VALUES(?, ?, ?, ?, ?, ?)" , (
52
+ str (FIRSTNAME .get ()), str (LASTNAME .get ()), str (GENDER .get ()), int (AGE .get ()), str (ADDRESS .get ()),
53
+ str (CONTACT .get ())))
54
+ conn .commit ()
55
+ cursor .execute ("SELECT * FROM `member` ORDER BY `lastname` ASC" )
56
+ fetch = cursor .fetchall ()
57
+ for data in fetch :
58
+ tree .insert ('' , 'end' , values = (data ))
59
+ cursor .close ()
60
+ conn .close ()
61
+ FIRSTNAME .set ("" )
62
+ LASTNAME .set ("" )
63
+ GENDER .set ("" )
64
+ AGE .set ("" )
65
+ ADDRESS .set ("" )
66
+ CONTACT .set ("" )
67
+
68
+
69
+ def UpdateData ():
70
+ if GENDER .get () == "" :
71
+ result = tkMessageBox .showwarning ('' , 'Please Complete The Required Field' , icon = "warning" )
72
+ else :
73
+ tree .delete (* tree .get_children ())
74
+ conn = sqlite3 .connect ("pythontut.db" )
75
+ cursor = conn .cursor ()
76
+ cursor .execute (
77
+ "UPDATE `member` SET `firstname` = ?, `lastname` = ?, `gender` =?, `age` = ?, `address` = ?, `contact` = ? WHERE `mem_id` = ?" ,
78
+ (str (FIRSTNAME .get ()), str (LASTNAME .get ()), str (GENDER .get ()), str (AGE .get ()), str (ADDRESS .get ()),
79
+ str (CONTACT .get ()), int (mem_id )))
80
+ conn .commit ()
81
+ cursor .execute ("SELECT * FROM `member` ORDER BY `lastname` ASC" )
82
+ fetch = cursor .fetchall ()
83
+ for data in fetch :
84
+ tree .insert ('' , 'end' , values = (data ))
85
+ cursor .close ()
86
+ conn .close ()
87
+ FIRSTNAME .set ("" )
88
+ LASTNAME .set ("" )
89
+ GENDER .set ("" )
90
+ AGE .set ("" )
91
+ ADDRESS .set ("" )
92
+ CONTACT .set ("" )
93
+
94
+
95
+ def OnSelected (event ):
96
+ global mem_id , UpdateWindow
97
+ curItem = tree .focus ()
98
+ contents = (tree .item (curItem ))
99
+ selecteditem = contents ['values' ]
100
+ mem_id = selecteditem [0 ]
101
+ FIRSTNAME .set ("" )
102
+ LASTNAME .set ("" )
103
+ GENDER .set ("" )
104
+ AGE .set ("" )
105
+ ADDRESS .set ("" )
106
+ CONTACT .set ("" )
107
+ FIRSTNAME .set (selecteditem [1 ])
108
+ LASTNAME .set (selecteditem [2 ])
109
+ AGE .set (selecteditem [4 ])
110
+ ADDRESS .set (selecteditem [5 ])
111
+ CONTACT .set (selecteditem [6 ])
112
+ UpdateWindow = Toplevel ()
113
+ UpdateWindow .title ("Contact List" )
114
+ width = 400
115
+ height = 300
116
+ screen_width = root .winfo_screenwidth ()
117
+ screen_height = root .winfo_screenheight ()
118
+ x = ((screen_width / 2 ) + 450 ) - (width / 2 )
119
+ y = ((screen_height / 2 ) + 20 ) - (height / 2 )
120
+ UpdateWindow .resizable (0 , 0 )
121
+ UpdateWindow .geometry ("%dx%d+%d+%d" % (width , height , x , y ))
122
+ if 'NewWindow' in globals ():
123
+ NewWindow .destroy ()
124
+
125
+ # ===================FRAMES==============================
126
+ FormTitle = Frame (UpdateWindow )
127
+ FormTitle .pack (side = TOP )
128
+ ContactForm = Frame (UpdateWindow )
129
+ ContactForm .pack (side = TOP , pady = 10 )
130
+ RadioGroup = Frame (ContactForm )
131
+ Male = Radiobutton (RadioGroup , text = "Male" , variable = GENDER , value = "Male" , font = ('arial' , 14 )).pack (side = LEFT )
132
+ Female = Radiobutton (RadioGroup , text = "Female" , variable = GENDER , value = "Female" , font = ('arial' , 14 )).pack (side = LEFT )
133
+
134
+ # ===================LABELS==============================
135
+ lbl_title = Label (FormTitle , text = "Updating Contacts" , font = ('arial' , 16 ), bg = "orange" , width = 300 )
136
+ lbl_title .pack (fill = X )
137
+ lbl_firstname = Label (ContactForm , text = "Firstname" , font = ('arial' , 14 ), bd = 5 )
138
+ lbl_firstname .grid (row = 0 , sticky = W )
139
+ lbl_lastname = Label (ContactForm , text = "Lastname" , font = ('arial' , 14 ), bd = 5 )
140
+ lbl_lastname .grid (row = 1 , sticky = W )
141
+ lbl_gender = Label (ContactForm , text = "Gender" , font = ('arial' , 14 ), bd = 5 )
142
+ lbl_gender .grid (row = 2 , sticky = W )
143
+ lbl_age = Label (ContactForm , text = "Age" , font = ('arial' , 14 ), bd = 5 )
144
+ lbl_age .grid (row = 3 , sticky = W )
145
+ lbl_address = Label (ContactForm , text = "Address" , font = ('arial' , 14 ), bd = 5 )
146
+ lbl_address .grid (row = 4 , sticky = W )
147
+ lbl_contact = Label (ContactForm , text = "Contact" , font = ('arial' , 14 ), bd = 5 )
148
+ lbl_contact .grid (row = 5 , sticky = W )
149
+
150
+ # ===================ENTRY===============================
151
+ firstname = Entry (ContactForm , textvariable = FIRSTNAME , font = ('arial' , 14 ))
152
+ firstname .grid (row = 0 , column = 1 )
153
+ lastname = Entry (ContactForm , textvariable = LASTNAME , font = ('arial' , 14 ))
154
+ lastname .grid (row = 1 , column = 1 )
155
+ RadioGroup .grid (row = 2 , column = 1 )
156
+ age = Entry (ContactForm , textvariable = AGE , font = ('arial' , 14 ))
157
+ age .grid (row = 3 , column = 1 )
158
+ address = Entry (ContactForm , textvariable = ADDRESS , font = ('arial' , 14 ))
159
+ address .grid (row = 4 , column = 1 )
160
+ contact = Entry (ContactForm , textvariable = CONTACT , font = ('arial' , 14 ))
161
+ contact .grid (row = 5 , column = 1 )
162
+
163
+ # ==================BUTTONS==============================
164
+ btn_updatecon = Button (ContactForm , text = "Update" , width = 50 , command = UpdateData )
165
+ btn_updatecon .grid (row = 6 , columnspan = 2 , pady = 10 )
166
+
167
+
168
+ # fn1353p
169
+ def DeleteData ():
170
+ if not tree .selection ():
171
+ result = tkMessageBox .showwarning ('' , 'Please Select Something First!' , icon = "warning" )
172
+ else :
173
+ result = tkMessageBox .askquestion ('' , 'Are you sure you want to delete this record?' , icon = "warning" )
174
+ if result == 'yes' :
175
+ curItem = tree .focus ()
176
+ contents = (tree .item (curItem ))
177
+ selecteditem = contents ['values' ]
178
+ tree .delete (curItem )
179
+ conn = sqlite3 .connect ("pythontut.db" )
180
+ cursor = conn .cursor ()
181
+ cursor .execute ("DELETE FROM `member` WHERE `mem_id` = %d" % selecteditem [0 ])
182
+ conn .commit ()
183
+ cursor .close ()
184
+ conn .close ()
185
+
186
+
187
+ def AddNewWindow ():
188
+ global NewWindow
189
+ FIRSTNAME .set ("" )
190
+ LASTNAME .set ("" )
191
+ GENDER .set ("" )
192
+ AGE .set ("" )
193
+ ADDRESS .set ("" )
194
+ CONTACT .set ("" )
195
+ NewWindow = Toplevel ()
196
+ NewWindow .title ("Contact List" )
197
+ width = 400
198
+ height = 300
199
+ screen_width = root .winfo_screenwidth ()
200
+ screen_height = root .winfo_screenheight ()
201
+ x = ((screen_width / 2 ) - 455 ) - (width / 2 )
202
+ y = ((screen_height / 2 ) + 20 ) - (height / 2 )
203
+ NewWindow .resizable (0 , 0 )
204
+ NewWindow .geometry ("%dx%d+%d+%d" % (width , height , x , y ))
205
+ if 'UpdateWindow' in globals ():
206
+ UpdateWindow .destroy ()
207
+
208
+ # ===================FRAMES==============================
209
+ FormTitle = Frame (NewWindow )
210
+ FormTitle .pack (side = TOP )
211
+ ContactForm = Frame (NewWindow )
212
+ ContactForm .pack (side = TOP , pady = 10 )
213
+ RadioGroup = Frame (ContactForm )
214
+ Male = Radiobutton (RadioGroup , text = "Male" , variable = GENDER , value = "Male" , font = ('arial' , 14 )).pack (side = LEFT )
215
+ Female = Radiobutton (RadioGroup , text = "Female" , variable = GENDER , value = "Female" , font = ('arial' , 14 )).pack (side = LEFT )
216
+
217
+ # ===================LABELS==============================
218
+ lbl_title = Label (FormTitle , text = "Add New Contact" , font = ('arial' , 16 ), bg = "#ffffff" , width = 300 )
219
+ lbl_title .pack (fill = X )
220
+ lbl_firstname = Label (ContactForm , text = "Firstname" , font = ('arial' , 14 ), bd = 5 )
221
+ lbl_firstname .grid (row = 0 , sticky = W )
222
+ lbl_lastname = Label (ContactForm , text = "Lastname" , font = ('arial' , 14 ), bd = 5 )
223
+ lbl_lastname .grid (row = 1 , sticky = W )
224
+ lbl_gender = Label (ContactForm , text = "Gender" , font = ('arial' , 14 ), bd = 5 )
225
+ lbl_gender .grid (row = 2 , sticky = W )
226
+ lbl_age = Label (ContactForm , text = "Age" , font = ('arial' , 14 ), bd = 5 )
227
+ lbl_age .grid (row = 3 , sticky = W )
228
+ lbl_address = Label (ContactForm , text = "Address" , font = ('arial' , 14 ), bd = 5 )
229
+ lbl_address .grid (row = 4 , sticky = W )
230
+ lbl_contact = Label (ContactForm , text = "Contact" , font = ('arial' , 14 ), bd = 5 )
231
+ lbl_contact .grid (row = 5 , sticky = W )
232
+
233
+ # ===================ENTRY===============================
234
+ firstname = Entry (ContactForm , textvariable = FIRSTNAME , font = ('arial' , 14 ))
235
+ firstname .grid (row = 0 , column = 1 )
236
+ lastname = Entry (ContactForm , textvariable = LASTNAME , font = ('arial' , 14 ))
237
+ lastname .grid (row = 1 , column = 1 )
238
+ RadioGroup .grid (row = 2 , column = 1 )
239
+ age = Entry (ContactForm , textvariable = AGE , font = ('arial' , 14 ))
240
+ age .grid (row = 3 , column = 1 )
241
+ address = Entry (ContactForm , textvariable = ADDRESS , font = ('arial' , 14 ))
242
+ address .grid (row = 4 , column = 1 )
243
+ contact = Entry (ContactForm , textvariable = CONTACT , font = ('arial' , 14 ))
244
+ contact .grid (row = 5 , column = 1 )
245
+
246
+ # ==================BUTTONS==============================
247
+ btn_addcon = Button (ContactForm , text = "Save" , width = 50 , command = SubmitData )
248
+ btn_addcon .grid (row = 6 , columnspan = 2 , pady = 10 )
249
+
250
+
251
+ # ============================FRAMES======================================
252
+ Top = Frame (root , width = 500 , bd = 1 , relief = SOLID )
253
+ Top .pack (side = TOP )
254
+ Mid = Frame (root , width = 500 , bg = "#1a1a1a" )
255
+ Mid .pack (side = TOP )
256
+ MidLeft = Frame (Mid , width = 100 )
257
+ MidLeft .pack (side = LEFT , pady = 10 )
258
+ MidLeftPadding = Frame (Mid , width = 370 , bg = "#1a1a1a" )
259
+ MidLeftPadding .pack (side = LEFT )
260
+ MidRight = Frame (Mid , width = 100 )
261
+ MidRight .pack (side = RIGHT , pady = 10 )
262
+ TableMargin = Frame (root , width = 500 )
263
+ TableMargin .pack (side = TOP )
264
+ # ============================LABELS======================================
265
+ lbl_title = Label (Top , text = "Contact Manager" , font = ('arial' , 16 ), width = 500 )
266
+ lbl_title .pack (fill = X )
267
+
268
+ # ============================ENTRY=======================================
269
+
270
+ # ============================BUTTONS=====================================
271
+ btn_add = Button (MidLeft , text = "+ ADD NEW" , bg = "#66ff66" , command = AddNewWindow )
272
+ btn_add .pack ()
273
+ btn_delete = Button (MidRight , text = "DELETE" , bg = "red" , command = DeleteData )
274
+ btn_delete .pack (side = RIGHT )
275
+
276
+ # ============================TABLES======================================
277
+ scrollbarx = Scrollbar (TableMargin , orient = HORIZONTAL )
278
+ scrollbary = Scrollbar (TableMargin , orient = VERTICAL )
279
+ tree = ttk .Treeview (TableMargin , columns = ("MemberID" , "Firstname" , "Lastname" , "Gender" , "Age" , "Address" , "Contact" ),
280
+ height = 400 , selectmode = "extended" , yscrollcommand = scrollbary .set , xscrollcommand = scrollbarx .set )
281
+ scrollbary .config (command = tree .yview )
282
+ scrollbary .pack (side = RIGHT , fill = Y )
283
+ scrollbarx .config (command = tree .xview )
284
+ scrollbarx .pack (side = BOTTOM , fill = X )
285
+ tree .heading ('MemberID' , text = "MemberID" , anchor = W )
286
+ tree .heading ('Firstname' , text = "Firstname" , anchor = W )
287
+ tree .heading ('Lastname' , text = "Lastname" , anchor = W )
288
+ tree .heading ('Gender' , text = "Gender" , anchor = W )
289
+ tree .heading ('Age' , text = "Age" , anchor = W )
290
+ tree .heading ('Address' , text = "Address" , anchor = W )
291
+ tree .heading ('Contact' , text = "Contact" , anchor = W )
292
+ tree .column ('#0' , stretch = NO , minwidth = 0 , width = 0 )
293
+ tree .column ('#1' , stretch = NO , minwidth = 0 , width = 0 )
294
+ tree .column ('#2' , stretch = NO , minwidth = 0 , width = 80 )
295
+ tree .column ('#3' , stretch = NO , minwidth = 0 , width = 120 )
296
+ tree .column ('#4' , stretch = NO , minwidth = 0 , width = 90 )
297
+ tree .column ('#5' , stretch = NO , minwidth = 0 , width = 80 )
298
+ tree .column ('#6' , stretch = NO , minwidth = 0 , width = 120 )
299
+ tree .column ('#7' , stretch = NO , minwidth = 0 , width = 120 )
300
+ tree .pack ()
301
+ tree .bind ('<Double-Button-1>' , OnSelected )
302
+
303
+ # ============================INITIALIZATION==============================
304
+ if __name__ == '__main__' :
305
+ Database ()
306
+ root .mainloop ()
0 commit comments