-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp22.py
659 lines (371 loc) · 16.3 KB
/
p22.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# Create GUI app with Python, Tkinter
# python tkinter tutorial
# python's standard library
# we can make GUI application using this library
# pronunciation: tee-kinter, tk-inter, kinter
# starter code
# import tkinter
# window=tkinter.Tk() # Tk is constructor(it will create the window)
# window.mainloop() # mainloop is method
# these three line will return window when we will run
# we must write these above three line when we want to make GUI application using Tkinter.
# import tkinter
# window=tkinter.Tk()
# window.title("GUI") # this will change the name of window
# window.mainloop()
# from tkinter import * # importing tkinter libray in other way
# window=Tk()
# window.title("GUI")
# window.mainloop()
# starter code
import tkinter as tk
from tkinter import ttk
window=tk.Tk()
window.title("GUI")
# create lables ---->lables can be a text, or lable of any thing(stuff)
# widgets(label, button, radio buttons) ---> available in tk and ttk library
# we can use tk or ttk for widgets but ttk widgets are more attractive in terms of design
# name_label=tk.Label(window,text="Enter Your name : ")
name_lable=ttk.Label(window,text="Enter Your name : ")
name_lable.grid(row=0,column=0,sticky=tk.W) # # there are two methos for set location of widget 1)pack (name_label.pack()), 2)grid(name_lable.grid(row=0,column=0))
email_lable=ttk.Label(window,text="Enter your E-mail : ")
email_lable.grid(row=1,column=0,sticky=tk.W)
age_label=ttk.Label(window,text="Enter your age : ")
age_label.grid(row=2,column=0,sticky=tk.W) # stiky=tk.W is used for start line from lest side window(stick leble on WEST side)
gender_label=ttk.Label(window,text="Gender : ")
gender_label.grid(row=3,column=0,sticky=tk.W)
radio_lable=ttk.Label(window,text='Your occuptionption : ')
radio_lable.grid(row=4,column=0,sticky=tk.W)
# Create Entry box
# name_entry=ttk.Entry(window,width=18)
# name_entry.grid(row=0,column=1)
name_var=tk.StringVar()
name_entry=ttk.Entry(window,width=18,textvariable=name_var) # what ever we will write in entry box, it will be stored in name_var
name_entry.grid(row=0,column=1)
name_entry.focus()
email_var=tk.StringVar()
email_entry=ttk.Entry(window,width=18,textvariable=email_var)
email_entry.grid(row=1,column=1)
age_var=tk.IntVar()
age_entry=ttk.Entry(window,width=18,textvariable=age_var)
age_entry.grid(row=2,column=1)
# Create Combo box
gender_var=tk.StringVar()
gender_combo=ttk.Combobox(window,width=15, textvariable=gender_var,state='readonly') # state is for make comobo box read only, it means we can not write inside combo box
gender_combo['values']=('Male','Female','Other')
gender_combo.current(0) # it is for set the value in comobo box
gender_combo.grid(row=3,column=1)
# create radio button
occuption_var=tk.StringVar()
radio_student=ttk.Radiobutton(window,text='Student', value='Student', variable=occuption_var)
radio_student.grid(row=4,column=1)
radio_teacher=ttk.Radiobutton(window,text='Teacher', value='Teacher', variable=occuption_var)
radio_teacher.grid(row=4,column=2)
# Create Checck Button
check_var=tk.IntVar()
check_button=ttk.Checkbutton(window,text="check if you want to subscribe to our newsletter",variable=check_var)
check_button.grid(row=5,columnspan=3) # columnspan is used for taking column without affecting other one. If we write only column we got affected GUI in terms of space.
# create button
# def action():
# user_name=name_var.get() # here, get() method is used for getting value of variable
# user_mail=email_var.get()
# user_age=age_var.get()
# user_gender=gender_var.get()
# user_occuption=occuption_var.get()
# # for check box, beccause check box will return 0 or 1.
# if check_var.get()==0:
# user_checked='NO'
# else:
# user_checked="YES"
# print(f'name:{user_name}, mail-id:{user_mail}, age:{user_age}, gender:{user_gender}, occuption:{user_occuption},Subscribed:{user_checked}')
# with open("data.txt","a") as data: # this code for stotre value in file
# data.write(f'{user_name},{user_mail},{user_age},{user_gender},{user_occuption},{user_checked} \n' )
# # these three lines for deleteing values from edit box after clicking on submit button
# name_entry.delete(0,tk.END)
# email_entry.delete(0,tk.END)
# age_entry.delete(0,tk.END)
# # when we want to change color of name lable after clicking on submit button
# name_lable.configure(foreground="Blue") # here we can use color code instead of color name
# # name_lable.configure(background="Blue")
# # window.configure(background="Pink") # change background color of window
# # submit_button.configure(foreground="Blue") # in this we will get error, for doing this we have to use stylish, otherwise use tk insted of ttk in submit_button=tk.Button(.....)
# write into CSV
from csv import DictWriter
import os
def action():
user_name=name_var.get() # here, get() method is used for getting value of variable
user_mail=email_var.get()
user_age=age_var.get()
user_gender=gender_var.get()
user_occuption=occuption_var.get()
# for check box, beccause check box will return 0 or 1.
if check_var.get()==0:
user_checked='NO'
else:
user_checked="YES"
print(f'name:{user_name}, mail-id:{user_mail}, age:{user_age}, gender:{user_gender}, occuption:{user_occuption},Subscribed:{user_checked}')
# write to csv
with open("data.csv","a",newline='') as data: # this code for stotre value in file
dict_write=DictWriter(data, fieldnames=['User_name','E_mail',"Age",'Gender','Occuption','Subscribed'])
if os.stat('data.csv').st_size==0:
dict_write.writeheader()
dict_write.writerow({
"User_name" : user_name,
"E_mail" : user_mail,
"Age" : user_age,
"Gender" : user_gender,
"Occuption" : user_occuption,
"Subscribed": user_checked
})
# these three lines for deleteing values from edit box after clicking on submit button
name_entry.delete(0,tk.END)
email_entry.delete(0,tk.END)
age_entry.delete(0,tk.END)
# when we want to change color of name lable after clicking on submit button
name_lable.configure(foreground="Blue")
submit_button=ttk.Button(window,text="Submit",command=action) # command is for calling method
submit_button.grid(row=6,column=0)
window.mainloop()
# Create Widgets using loop
# For loop
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
win.title("Loop")
# name_lable=ttk.Label(win,text="Enter your name:")
# name_lable.grid(row=0,column=0,sticky=tk.W)
# create lables using loop
lables=['Enter Your Name : ','Enter Your Email : ','Enter Your Age : ','Gender : ','Country : ','State : ','City : ']
for i in range(len(lables)):
currunt_name='lable'+'f{i}'
currunt_name=ttk.Label(win,text=lables[i])
currunt_name.grid(row=i,column=0,sticky=tk.W)
# create entry box
# name_var=tk.StringVar()
user_info={
'name':tk.StringVar(),
'email':tk.StringVar(),
'age':tk.StringVar(),
'gender':tk.StringVar(),
'country':tk.StringVar(),
'state':tk.StringVar(),
'city':tk.StringVar()
}
count=0
for i in user_info:
entry_var=i+'_entry'
entry_var=ttk.Entry(win, width=16,textvariable=user_info[i])
entry_var.grid(column=1,row=count)
count+=1
# name_entry=tk.Entry(win, width=16,textvariable=name_var)
# name_entry.grid(row=0,column=1)
# Button
def submit():
for i in user_info:
print(f"my {i} is {user_info[i].get()}.")
button_entry=ttk.Button(win,text='Submit',command=submit)
button_entry.grid(row=count+1,columnspan=3)
win.mainloop()
# Padding and Lable Frame
# Padding
# # Create Widgets using loop
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
win.title("Padding")
# create lables using loop
lables=['Enter Your Name : ','Enter Your Email : ','Enter Your Age : ','Gender : ','Country : ','State : ','City : ']
for i in range(len(lables)):
currunt_name='lable'+'f{i}'
currunt_name=ttk.Label(win,text=lables[i])
currunt_name.grid(row=i,column=0,sticky=tk.W, padx=40, pady=10) # padx for Left to Right padding and pady for top and bottom padding
# if i will givepadding in only lables, i will get padding automatically in entry boxes also
# create entry box
user_info={
'name':tk.StringVar(), # here key is var name
'email':tk.StringVar(),
'age':tk.StringVar(),
'gender':tk.StringVar(),
'country':tk.StringVar(),
'state':tk.StringVar(),
'city':tk.StringVar()
}
count=0
for i in user_info:
entry_var=i+'_entry'
entry_var=ttk.Entry(win, width=16,textvariable=user_info[i])
entry_var.grid(column=1,row=count, padx=40, pady=10)
count+=1
# Button
def submit1():
for i in user_info:
print(f"my {i} is {user_info[i].get()}.")
button_entry=ttk.Button(win,text='Submit',command=submit1)
button_entry.grid(row=count+1,columnspan=3, padx=40, pady=10)
win.mainloop()
# Lable Frame
# at this time, we are using our full window, we add all lables and entry boxes on our manin window(main frame)
# But now we will make our window(frame) using lable frame
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
win.title(" Lable Frame ")
lable_frame=ttk.LabelFrame(win,text="Enter Your Details Below")
lable_frame.grid(row=0,column=0, padx=50, pady=50)
# if we will run our gui now, we can not see our frame, i mean we can not see our added frame until we add lables and entry boxes in our frame
# create lables using loop
lables=['Enter Your Name : ','Enter Your Email : ','Enter Your Age : ','Gender : ','Country : ','State : ','City : ']
for i in range(len(lables)):
currunt_name='lable'+'f{i}'
currunt_name=ttk.Label(lable_frame,text=lables[i])
currunt_name.grid(row=i,column=0,sticky=tk.W)
# create entry box
user_info={
'name':tk.StringVar(), # here key is var name
'email':tk.StringVar(),
'age':tk.StringVar(),
'gender':tk.StringVar(),
'country':tk.StringVar(),
'state':tk.StringVar(),
'city':tk.StringVar()
}
count=0
for i in user_info:
entry_var=i+'_entry'
entry_var=ttk.Entry(lable_frame, width=16,textvariable=user_info[i])
entry_var.grid(column=1,row=count)
count+=1
# Button
def submit2():
for i in user_info:
print(f"my {i} is {user_info[i].get()}.")
button_entry=ttk.Button(lable_frame,text='Submit',command=submit2)
button_entry.grid(row=count+1,columnspan=3)
# In this case we haven't added padding
# don't worry, we have one method of lable_frame_widget like
for child in lable_frame.winfo_children():
# print(child) # in this i will get list of all lables and entries and button which are in frame
child.grid_configure(padx=40,pady=10)
win.mainloop()
# Tabbed Control Widget
# Tab Widget
# Notebook -----> contain twwo pages
# Page 1 # Page 2
# Widgets Widgets
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
win.title(" Tab Widget ")
nb=ttk.Notebook(win)
# Page1 and page2 both will frames
page1=ttk.Frame(nb)
page2=ttk.Frame(nb)
# I have to add these two pages in Notebook
nb.add(page1,text='ONE')
nb.add(page2,text='TWO')
# I want to add this complete notebook on window
# nb.grid(row=0,column=0)
nb.pack(expand=True, fill='both')
# Expand ---> when set to true, widget expands to fill any space not otherwise used in widget's parent
# fill ---> Determines whether widget fills any extra space allocated to it by packer, or keeps its own minimal dimensions: NONE(default), X)fill only horizontally),Y (fill only vertically), or BOTH(fill both Horizontally and vertically)
# I will create lable and entrybox on page1
lable_page1=ttk.Label(page1,text="Enter Your Name : ")
lable_page1.grid(row=0,column=0)
entry_page1=ttk.Entry(page1,width=30)
entry_page1.grid(row=0,column=1)
lable_page2=ttk.Label(page2,text="Enter Your Age : ")
lable_page2.grid(row=0,column=0)
win.mainloop()
# Create MenuBar
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
win.title(" MenuBar ")
def func():
print("func called....")
# Menu ---> this is a class in our tkinter
# ************************ Simple MenuBar ***********************
# menubar=tk.Menu(win)
# menubar.add_command(label='Save', command=func)
# menubar.add_command(label='Save As', command=func)
# menubar.add_command(label='Copy', command=func)
# menubar.add_command(label='Past', command=func)
main_menu=tk.Menu(win)
# File Menu
file_menu=tk.Menu(main_menu,tearoff=0) # tearoff is used to remove tearoff (---------) which hapenes in first line of file and edit
# Now i want to add command(labels) in File
file_menu.add_command(label='New File', command=func)
file_menu.add_command(label='New Window', command=func)
# to create saperator between two command
file_menu.add_separator()
file_menu.add_command(label='Open File', command=func)
file_menu.add_command(label='Save File', command=func)
# Now we have to cascade file_menu in main_menu
main_menu.add_cascade(label="File", menu=file_menu)
# Edit menu
edit_menu=tk.Menu(main_menu,tearoff=0)
# Now i want to add command(labels) in File
edit_menu.add_command(label='Undo', command=func)
edit_menu.add_command(label='Redo', command=func)
edit_menu.add_separator()
edit_menu.add_command(label='Cut', command=func)
edit_menu.add_command(label='Copy', command=func)
edit_menu.add_command(label='Past', command=func)
edit_menu.add_separator()
edit_menu.add_command(label='Find', command=func)
# Now we have to cascade edit_menu in main_menu
main_menu.add_cascade(label="Edit", menu=edit_menu)
# here we will not use grid or pack
win.config(menu=main_menu)
win.mainloop()
# Message Box and Exception Handling
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox as m_box
win=tk.Tk()
win.title(" Message Box ")
# Label frame
lable_frame=ttk.LabelFrame(win,text="Contact Detail")
lable_frame.grid(row=0,column=0,padx=40,pady=40)
# Create labels and entry in label frame
# ************* For name ***************
# name label
name_label=ttk.Label(lable_frame,text="Enter Your name : ",font=('Helvetica',14,'bold')) # font is used to make deal with label's font, helvetica comes from font family, 14 is font size.
name_label.grid(row=0,column=0,padx=10,pady=10,sticky=tk.W)
# name entry
name_var=tk.StringVar()
name_entry=ttk.Entry(lable_frame,width=36,textvariable=name_var)
name_entry.grid(row=1,column=0,padx=10,pady=10,sticky=tk.W)
# ************ For age **************
# age label
age_label=ttk.Label(lable_frame,text="Enter Your age : ",font=('Helvetica',14))
age_label.grid(row=0,column=1,padx=10,pady=10,sticky=tk.W)
# age entry
age_var=tk.StringVar()
age_entry=ttk.Entry(lable_frame,width=36,textvariable=age_var)
age_entry.grid(row=1,column=1,padx=10,pady=10,sticky=tk.W)
# ********** Button **************
def action1():
# m_box.showinfo('title','content of this message box !!')
# m_box.showerror('title','content of this message box !!!')
# m_box.showwarning('title','content of this message box !!!')
name=name_var.get()
age=age_var.get()
if name=='' or age=='':
m_box.showerror('Error','Please fill both entries !!!')
else:
# age='wvvsdsc' # Value error
try:
age=int(age)
except ValueError:
m_box.showerror("Error","Only digits are allowed in age field")
else:
if age>0:
print(f'name is : {name} and age is : {age}')
else:
raise ValueError(m_box.showerror('Error','Please enter valid age'))
if age<18:
m_box.showwarning("warning","you are not 18, visit this content on your own risk")
sub_btn=ttk.Button(win,text="Submit", command=action1)
sub_btn.grid(row=1,columnspan=3,padx=40)
win.mainloop()