├── README.md
└── exercises
├── .idea
├── encodings.xml
├── exercises.iml
├── misc.xml
├── modules.xml
└── workspace.xml
├── Module 1
├── a0.py
├── event.py
├── hello.py
├── hello2.py
└── template.py
├── Module 2
├── button.py
├── checkbutton.py
├── combobox.py
├── entry.py
├── label-config.py
├── label.py
├── menu.py
├── messagebox.py
├── progressbar.py
├── radiobuttons.py
├── scale.py
├── scalebar.py
├── scrollbar.py
├── spinbox.py
├── style.py
├── text.py
├── treeview.py
└── widgets.py
├── Module 3
├── frame.py
├── notebook.py
├── panedwindow.py
└── toplevel.py
├── Module 4
├── grid.py
├── pack.py
└── place.py
├── Module 5
└── 06_canvas.py
├── Module 6
├── callback.py
├── event-test.py
├── keyboard.py
├── mouse.py
├── mouse_challenge.py
├── multiple.py
└── virtual.py
├── Module 7
├── app.py
├── feedback_solution.py
├── feedback_template.py
├── feedback_template_with_events.py
├── feedback_template_with_layout.py
└── feedback_template_with_widgets.py
└── images
├── 0922.py
├── python_logo.gif
└── tour_logo.gif
/README.md:
--------------------------------------------------------------------------------
1 | # Python GUI Development with Tkinter Training
2 | ## by [Tertiary Infotech Pte. Ltd](https://www.tertiarycourses.com.sg/)
3 |
4 | These are the exercise files used for [Python GUI Development with Tkinter Trainings](https://www.tertiarycourses.com.sg/python-gui-development-with-tkinter.html) course.
5 |
6 | The course outline can be found in
7 |
8 | https://www.tertiarycourses.com.sg/python-gui-development-with-tkinter.html
9 |
10 |
Module 1 Get Started
11 |
12 | - What is Tkinter
13 | - Installing and Importing Tkinter
14 | - Basic Tkinter Concepts
15 | - First Tkinter Program
16 | - Second Tkinter Program
17 |
18 | Module 2: Widgets
19 |
20 | - Label
21 | - Button
22 | - Check Button
23 | - Radio Button
24 | - Entry
25 | - Text
26 | - Combo Box
27 | - Spin Box
28 | - Progress Bar
29 | - Scale Bar
30 | - Treeview
31 | - Scroll bar
32 | - Message Box
33 | - Widget Styles
34 |
35 | Module 3 Organizing Widgets
36 |
37 | - Frame
38 | - Top Level Window
39 | - Paned Window
40 | - Notebook
41 |
42 | Module 4 Geometry Managers
43 |
44 | - Pack
45 | - Grid
46 | - Place
47 |
48 | Module 5 Shapes
49 |
50 | - Canvas
51 | - Line
52 | - Rect
53 | - Oval
54 | - Arc
55 | - Poly
56 | - Text
57 |
58 | Module 6 Event Handling
59 |
60 | - Event Handling Methods
61 | - Command Callbacks
62 | - Binding to Keyboard Event
63 | - Binding to Mouse Event
64 | - Binding to Virtual Event
65 | - Binding to Multiple Events
66 |
67 | Module 7 Tkinter GUI Demo
68 |
69 | - Best Practices
70 | - Layout of a Survey Form
71 | - Step 1: Feedback Canvas
72 | - Step 2: Create Widgets
73 | - Step 3: Layout Widgets
74 | - Step 4: Binding to Events
75 |
76 |
77 |
--------------------------------------------------------------------------------
/exercises/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/exercises/.idea/exercises.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/exercises/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/exercises/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/exercises/.idea/workspace.xml:
--------------------------------------------------------------------------------
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 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | Buildout
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 | 1467782060113
319 |
320 |
321 | 1467782060113
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 |
--------------------------------------------------------------------------------
/exercises/Module 1/a0.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | import numpy as np
3 | import matplotlib.pyplot as plt
4 |
5 | root = Tk()
6 |
7 | # w = Label(root, text="Hello, world!")
8 | # w.pack()
9 | # root.mainloop()
10 |
11 | w = Scale(root,from_= 0,to = 100, orient = HORIZONTAL)
12 | w.pack()
13 | root.mainloop()
14 |
15 | #root.geometry('800x600')
16 | # c = Canvas(root,width=800, height=600)
17 | # c.pack()
18 |
19 | # r = c.create_rectangle(0,0,50,50,fill='red', outline='red')
20 |
--------------------------------------------------------------------------------
/exercises/Module 1/event.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | class HelloApp:
4 |
5 | def __init__(self, root):
6 |
7 | self.label = Label(root, text = "Who are you")
8 | self.label.grid(row = 0, column = 0)
9 |
10 | Button(root, text = "Click Me", command = self.update).grid(row = 1, column = 0)
11 |
12 |
13 | def update(self):
14 | self.label.config(text = 'I am Alfred')
15 |
16 |
17 | def main():
18 |
19 | root = Tk()
20 | app = HelloApp(root)
21 | root.mainloop()
22 |
23 | if __name__ == "__main__": main()
24 |
25 |
26 |
--------------------------------------------------------------------------------
/exercises/Module 1/hello.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 | Label(root, text="Hello, Tkinter!").pack()
5 |
6 | root.mainloop()
7 |
--------------------------------------------------------------------------------
/exercises/Module 1/hello2.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | def printHello():
4 | print("Hello")
5 |
6 | root = Tk()
7 |
8 | Button(root,text='Click Me', command = printHello).pack()
9 |
10 | root.mainloop()
11 |
--------------------------------------------------------------------------------
/exercises/Module 1/template.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 |
5 | # code goes here
6 |
7 | root.mainloop()
8 |
--------------------------------------------------------------------------------
/exercises/Module 2/button.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 | #w = Button(root, text="Click Me!")
6 | w = ttk.Button(root, text="Click Me!")
7 | w.pack()
8 |
9 | # w['text'] = "Push Me"
10 | # w.config(text="Push Me")
11 |
12 | # def callback():
13 | # print('Clicked!')
14 | # w.config(command = callback)
15 |
16 | # w.state(['disabled'])
17 | # w.instate(['disabled'])
18 |
19 | # w.state(['!disabled'])
20 | # w.instate(['!disabled'])
21 |
22 | # logo = PhotoImage(file = 'python_logo.gif')
23 | # w.config(image = logo, compound = LEFT)
24 | # small_logo = logo.subsample(5, 5)
25 | # w.config(image = small_logo)
26 |
27 |
28 | root.mainloop()
--------------------------------------------------------------------------------
/exercises/Module 2/checkbutton.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 |
5 | s = StringVar()
6 | s.set('Name')
7 |
8 | w = Checkbutton(root, text = 'Who?')
9 | w.pack()
10 |
11 | w.config(variable = s, onvalue = 'Alfred', offvalue = 'Ally')
12 | mainloop()
13 |
14 | print(s.get())
15 |
16 |
--------------------------------------------------------------------------------
/exercises/Module 2/combobox.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | month = StringVar()
7 | combobox = ttk.Combobox(root, textvariable = month)
8 | combobox.pack()
9 | combobox.config(values = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
10 | 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
11 | month.set('Dec')
12 | # month.set('Not a month!')
13 |
14 | # year = StringVar()
15 | # Spinbox(root, from_ = 1990, to = 2014, textvariable = year).pack()
16 | # print(year.get())
17 |
18 | root.mainloop()
19 | print(month.get())
--------------------------------------------------------------------------------
/exercises/Module 2/entry.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 |
5 | v = StringVar()
6 | w = Entry(root, width = 30, textvariable=v).pack()
7 |
8 | # w.delete(0, 1)
9 | # w.delete(0, END)
10 |
11 | # w.insert(0, 'Enter your password')
12 | # w.config(show = '*')
13 | #w.state(['disabled'])
14 | # w.state(['readonly'])
15 | # w.state(['!disabled'])
16 |
17 | root.mainloop()
18 | print(v.get())
19 |
--------------------------------------------------------------------------------
/exercises/Module 2/label-config.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 | label = Label(root, text="Hello, Today is a great day. What have you been doing")
5 | `
6 | label.pack()
7 | root.mainloop()
8 |
--------------------------------------------------------------------------------
/exercises/Module 2/label.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 | #w = Label(root, text="Hello, Tkinter!")
6 | w = ttk.Label(root, text="Hello, Tkinter!")
7 | w.pack()
8 |
9 | # w.config(wraplength = 150)
10 | # w.config(foreground = 'blue')
11 | # w.config(background = 'yellow')
12 | # w.config(font = ('Courier',18,'bold'))
13 |
14 | # logo = PhotoImage(file = 'python_logo.gif')
15 | # w.config(image = logo)
16 | # w.config(compound = 'text')
17 | # w.config(compound = 'center')
18 | # w.config(compound = 'left')
19 |
20 | root.mainloop()
21 |
--------------------------------------------------------------------------------
/exercises/Module 2/menu.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | root.option_add('*tearOff', False)
7 | menubar = Menu(root)
8 | root.config(menu = menubar)
9 | file = Menu(menubar)
10 | edit = Menu(menubar)
11 | help_ = Menu(menubar)
12 |
13 | menubar.add_cascade(menu = file, label = 'File')
14 | menubar.add_cascade(menu = edit, label = 'Edit')
15 | menubar.add_cascade(menu = help_, label = 'Help')
16 |
17 | # file.add_command(label = 'New', command = lambda: print('New File'))
18 | # file.add_separator()
19 | # file.add_command(label = 'Open...', command = lambda: print('Opening File...'))
20 | # file.add_command(label = 'Save', command = lambda: print('Saving File...'))
21 |
22 | # file.entryconfig('New', accelerator = 'Ctrl+N')
23 | # logo = PhotoImage(file = 'C:\\Users\\barron\\Dropbox\\Lynda Courses\\Python GUI Development with Tkinter\\Exercise Files - Current\\03 Widget Classes\\python_logo.gif').subsample(10, 10)
24 | # file.entryconfig('Open...', image = logo, compound = 'left')
25 | # file.entryconfig('Open...', state = 'disabled')
26 |
27 | # file.delete('Save')
28 | # save = Menu(file)
29 | # file.add_cascade(menu = save, label = 'Save')
30 | # save.add_command(label = 'Save As',command = lambda: print('Saving As...'))
31 | # save.add_command(label = 'Save All', command = lambda: print('Saving All...'))
32 |
33 | # choice = IntVar()
34 | # edit.add_radiobutton(label = 'One', variable = choice, value = 1)
35 | # edit.add_radiobutton(label = 'Two', variable = choice, value = 2)
36 | # edit.add_radiobutton(label = 'Three', variable = choice, value = 3)
37 | # file.post(400, 300)
38 |
39 | root.mainloop()
40 |
--------------------------------------------------------------------------------
/exercises/Module 2/messagebox.py:
--------------------------------------------------------------------------------
1 | from tkinter import messagebox
2 |
3 | #messagebox.showinfo(title = "A Message", message = 'Hello!')
4 | print(messagebox.askyesno(title = 'Question?', message = 'You OK?'))
5 |
6 | # from tkinter import filedialog
7 | # filename = filedialog.askopenfile()
8 | # print(filename.name)
9 |
10 | # from tkinter import colorchooser
11 | # print(colorchooser.askcolor(initialcolor = "#FFFFFF"))
12 |
--------------------------------------------------------------------------------
/exercises/Module 2/progressbar.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | w = ttk.Progressbar(root, orient = HORIZONTAL, length = 200)
7 | w.pack()
8 |
9 | w.config(mode = 'indeterminate')
10 | w.start()
11 | w.stop()
12 |
13 | # w.config(mode = 'determinate', maximum = 11.0, value = 4.2)
14 | # w.config(value = 8.0)
15 | w.step()
16 | w.step(5)
17 |
18 | # value = DoubleVar()
19 | # w.config(variable = value)
20 |
21 | # scale = ttk.Scale(root, orient = HORIZONTAL,
22 | # length = 400, variable = value,
23 | # from_ = 0.0, to = 11.0)
24 | # scale.pack()
25 | # scale.set(4.2)
26 | # print(scale.get())
27 |
28 | root.mainloop()
29 |
--------------------------------------------------------------------------------
/exercises/Module 2/radiobuttons.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 |
5 | num = IntVar()
6 |
7 | Radiobutton(root, text = '1', variable = num,
8 | value = '1').pack()
9 | Radiobutton(root, text = '2', variable = num,
10 | value = '2').pack()
11 | Radiobutton(root, text = '3', variable = num,
12 | value = '3').pack()
13 | Radiobutton(root, text = '1+', variable = num,
14 | value = '1').pack()
15 |
16 | mainloop()
17 | print(num.get())
18 |
19 |
--------------------------------------------------------------------------------
/exercises/Module 2/scale.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 |
5 | def main():
6 | w = Scale(root,from_= 0,to = 100, orient = HORIZONTAL, command = update)
7 | w.pack()
8 | root.mainloop()
9 |
10 | def update(duty):
11 | pass
12 | #pwm.ChangeDutyCycle(float(duty))
13 |
14 | if __name__ == "__main__":main()
15 |
--------------------------------------------------------------------------------
/exercises/Module 2/scalebar.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | value = DoubleVar()
7 |
8 | def update(value):
9 | print(value)
10 |
11 | # w = ttk.Scale(root, orient = HORIZONTAL,
12 | # length = 400, variable = value,
13 | # from_ = 0.0, to = 11.0)
14 | w = ttk.Scale(root, orient = HORIZONTAL,
15 | length = 400, variable = value,
16 | from_ = 0.0, to = 11.0,command=update)
17 | w.pack()
18 | #w.set(4.2)
19 |
20 | root.mainloop()
21 | print(value.get())
--------------------------------------------------------------------------------
/exercises/Module 2/scrollbar.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | scrollbar = Scrollbar(root)
7 | scrollbar.pack(side=RIGHT, fill=Y)
8 |
9 | text = Text(root, yscrollcommand=scrollbar.set)
10 | for i in range(1000):
11 | text.insert(END, str(i))
12 | text.pack(side=LEFT, fill=BOTH)
13 |
14 | scrollbar.config(command=text.yview)
15 |
16 |
17 | # w = Text(root, width = 400, height = 100)
18 | # w.pack()
19 | # #canvas = Canvas(root, scrollregion = (0, 0, 640, 480), bg = 'white')
20 | # xscroll = ttk.Scrollbar(root, orient = HORIZONTAL)
21 | # yscroll = ttk.Scrollbar(root, orient = VERTICAL)
22 | # w.config(xscrollcommand = xscroll.set, yscrollcommand = yscroll.set)
23 |
24 | # canvas.grid(row = 1, column = 0)
25 | # xscroll.grid(row = 2, column = 0, sticky = 'ew')
26 | # yscroll.grid(row = 1, column = 1, sticky = 'ns')
27 |
28 | # def canvas_click(event):
29 | # x = canvas.canvasx(event.x)
30 | # y = canvas.canvasy(event.y)
31 | # canvas.create_oval((x-5, y-5, x+5, y+5), fill = 'green')
32 |
33 | # canvas.bind('<1>', canvas_click)
34 |
35 | root.mainloop()
36 |
--------------------------------------------------------------------------------
/exercises/Module 2/spinbox.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | year = StringVar()
7 | Spinbox(root, from_ = 1990, to = 2014, textvariable = year).pack()
8 |
9 | root.mainloop()
10 | print(year.get())
11 |
--------------------------------------------------------------------------------
/exercises/Module 2/style.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | button1 = ttk.Button(root, text = 'Button 1')
7 | button2 = ttk.Button(root, text = 'Button 2')
8 | button1.pack()
9 | button2.pack()
10 |
11 | style = ttk.Style()
12 |
13 | # print(style.theme_names())
14 | # print(style.theme_use())
15 | #style.theme_use('default')
16 | style.theme_use('classic')
17 | #style.theme_use('vista')
18 |
19 | # print(button1.winfo_class())
20 | style.configure('TButton', foreground = 'blue')
21 | style.configure('TButton', background = 'yellow')
22 | # style.configure('Alarm.TButton', foreground = 'orange',
23 | # font = ('Arial', 24, 'bold'))
24 | # button2.configure(style = 'Alarm.TButton')
25 | # style.map('Alarm.TButton', foreground = [('pressed', 'pink'),
26 | # ('disabled', 'grey')])
27 | # button2.state(['disabled'])
28 |
29 | # print(style.layout('TButton'))
30 | # print(style.element_options('Button.label'))
31 | # print(style.lookup('TButton', 'foreground'))
32 |
33 | root.mainloop()
34 |
--------------------------------------------------------------------------------
/exercises/Module 2/text.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | s = StringVar()
7 | w = Text(root, width = 40, height = 10)
8 | w.pack()
9 | w.config(wrap = 'word')
10 | text= w.get('1.0', 'end')
11 | print(w.get('1.0', '1.end'))
12 | # w.insert('1.0 + 2 lines', 'Inserted message')
13 | # w.insert('1.0 + 2 lines lineend', ' and\nmore and\nmore.')
14 | # w.delete('1.0')
15 | # w.delete('1.0', '1.0 lineend')
16 | # w.delete('1.0', '3.0 lineend + 1 chars')
17 | # w.replace('1.0', '1.0 lineend', 'This is the first line.')
18 |
19 | # w.config(state = 'disabled')
20 | # w.delete('1.0', 'end')
21 | # w.config(state = 'normal')
22 |
23 | # w.tag_add('my_tag', '1.0', '1.0 wordend')
24 | # w.tag_configure('my_tag', background = 'yellow')
25 | # wt.tag_remove('my_tag', '1.1', '1.3')
26 | # print(w.tag_ranges('my_tag'))
27 | # print(tw.tag_names())
28 | # w.replace('my_tag.first', 'my_tag.last', 'That was')
29 | # w.tag_delete('my_tag')
30 |
31 | # w.mark_names()
32 | # w.insert('insert', '_')
33 | # w.mark_set('my_mark', 'end')
34 | # w.mark_gravity('my_mark', 'right')
35 | # text.mark_unset('my_mark')
36 |
37 | # image = PhotoImage(file = 'python_logo.gif').subsample(5, 5) # Change path as needed
38 | # w.image_create('insert', image = image)
39 | # w.image_create('insert', image = image)
40 |
41 | # button = Button(text, text = 'Click Me')
42 | # text.window_create('insert', window = button)
43 |
44 | root.mainloop()
45 | print(text)
46 |
--------------------------------------------------------------------------------
/exercises/Module 2/treeview.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | w = ttk.Treeview(root)
7 | w.pack()
8 | w.insert('', '0', 'item1', text = 'First Item')
9 | w.insert('', '1', 'item2', text = 'Second Item')
10 | w.insert('', 'end', 'item3', text = 'Third Item')
11 |
12 | # logo = PhotoImage(file = 'C:\\Users\\barron\\Dropbox\\Lynda Courses\\Python GUI Development with Tkinter\\Exercise Files - Current\\03 Widget Classes\\python_logo.gif').subsample(10,10)
13 | # w.insert('item2', 'end', 'python', text = 'Python', image = logo)
14 |
15 | # w.config(height = 5)
16 | w.move('item2', 'item1', 'end')
17 | w.item('item1', open = True)
18 | # w.item('item2', open = True)
19 | #print(w.item('item1', 'open'))
20 |
21 | #w.detach('item3')
22 | #w.move('item3', 'item2', '0')
23 | w.delete('item3')
24 |
25 | # treeview.configure(column = ('version'))
26 | # treeview.column('version', width = 50, anchor = 'center')
27 | # treeview.column('version', width = 50, anchor = 'center')
28 | # treeview.column('#0', width = 150)
29 | # treeview.heading('version', text = 'Version')
30 | # treeview.set('python', 'version', '3.4')
31 | # treeview.item('python', tags = ('software'))
32 | # treeview.tag_configure('software', background = 'yellow')
33 |
34 | # def callback(event):
35 | # print(treeview.selection())
36 |
37 | # treeview.bind('<>', callback)
38 |
39 | # treeview.config(selectmode = 'browse')
40 | # print(treeview.selection_add('python'))
41 | # print(treeview.selection_remove('python'))
42 | # print(treeview.selection_toggle('python'))
43 |
44 | root.mainloop()
45 |
--------------------------------------------------------------------------------
/exercises/Module 2/widgets.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | button = ttk.Button(root, text = 'Click Me')
7 | button.pack()
8 |
9 | print(button['text'])
10 | button['text'] = 'Press Me'
11 | button.config(text = 'Push Me')
12 | print(button.config())
13 |
14 | print(str(button))
15 | print(str(root))
16 |
17 | ttk.Label(root, text ='Hello, Tkinter!').pack()
18 |
19 | # mainloop() add
20 | root.mainloop()
21 |
--------------------------------------------------------------------------------
/exercises/Module 3/frame.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | # f1 = ttk.Frame(root)
7 | # f1.pack()
8 | # f1.config(height = 100, width = 200)
9 | # f1.config(relief = RIDGE)
10 | # f2 = ttk.Frame(root)
11 | # f2.pack()
12 | # f2.config(height = 300, width = 400)
13 | # f2.config(relief = SUNKEN)
14 |
15 | # Button(f1, text = 'Click Me on Frame 1').pack()
16 | # Button(f2, text = 'Click Me on Frame 2').pack()
17 | #f1.config(padding = (300, 150))
18 | ttk.LabelFrame(root, height = 100, width = 200, text = 'My Frame').pack()
19 |
20 | root.mainloop()
21 |
--------------------------------------------------------------------------------
/exercises/Module 3/notebook.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | w = ttk.Notebook(root)
7 | w.pack()
8 |
9 | frame1 = ttk.Frame(w)
10 | frame2 = ttk.Frame(w)
11 | w.add(frame1, text = 'One')
12 | w.add(frame2, text = 'Two')
13 | ttk.Button(frame1, text = 'Click Me').pack()
14 |
15 | frame3 = ttk.Frame(w)
16 | w.insert(1, frame3, text = 'Three')
17 | # notebook.forget(1)
18 | # notebook.add(frame3, text = 'Three')
19 |
20 | w.select(2)
21 | print(w.index(w.select()))
22 | # w.select(2)
23 |
24 | w.tab(1, state = 'disabled')
25 | # w.tab(1, state = 'hidden')
26 | # w.tab(1, state = 'normal')
27 | # w.tab(1, 'text')
28 | # w.tab(1)
29 |
30 | root.mainloop()
31 |
--------------------------------------------------------------------------------
/exercises/Module 3/panedwindow.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | # w = PanedWindow(orient=VERTICAL)
7 | # w.pack(fill=BOTH, expand=1)
8 |
9 | # top = Label(w, text="top pane")
10 | # w.add(top)
11 |
12 | # bottom = Label(w, text="bottom pane")
13 | # w.add(bottom)
14 |
15 |
16 | w = ttk.PanedWindow(orient = HORIZONTAL)
17 | w.pack(fill = BOTH, expand = True)
18 |
19 | f1 = ttk.Frame(w, width = 100, height = 300, relief = SUNKEN)
20 | f2 = ttk.Frame(w, width = 400, height = 400, relief = SUNKEN)
21 | w.add(f1, weight = 1)
22 | w.add(f2, weight = 4)
23 |
24 | f3 = ttk.Frame(w, width = 50, height = 50, relief = SUNKEN)
25 | w.insert(1, f3)
26 |
27 | top = Label(f1, text="top pane")
28 | top.pack()
29 |
30 | # bottom = Label(w, text="bottom pane")
31 | # w.add(bottom)
32 |
33 | # panedwindow.forget(1)
34 |
35 | root.mainloop()
36 |
--------------------------------------------------------------------------------
/exercises/Module 3/toplevel.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 |
5 | w = Toplevel(root)
6 | w.title('New Window')
7 |
8 | #w.lower()
9 | w.lift(root)
10 |
11 | # w.state('zoomed')
12 | # w.state('withdrawn')
13 | # w.state('iconic')
14 | # w.state('normal')
15 | # print(w.state())
16 | # wi.state('normal')
17 |
18 | # window.iconify()
19 | # window.deiconify()
20 |
21 | #w.geometry('640x480+50+100')
22 | # print(window.geometry())
23 | # window.resizable(False, False)
24 | # window.maxsize(640, 480)
25 | # window.minsize(200, 200)
26 | # window.resizable(True, True)
27 |
28 | # root.destroy()
29 |
30 | root.mainloop()
31 |
--------------------------------------------------------------------------------
/exercises/Module 4/grid.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 |
5 | # Label(root, text = 'Green', background = 'Green').grid(row = 0, column = 0)
6 | # Label(root, text = 'Orange', background = 'Orange').grid(row = 0, column = 1)
7 | # Label(root, text = 'Blue', background = 'Blue').grid(row = 1, column = 0)
8 | # Label(root, text = 'Yellow', background = 'Yellow').grid(row = 1, column = 1)
9 |
10 | Label(root, text = 'Green', background = 'Green').grid(row = 0, column = 0)
11 | Label(root, text = 'Orange', background = 'Orange').grid(row = 0, column = 1)
12 | Label(root, text = 'Yellow', background = 'yellow').grid(row = 0, column = 2, rowspan = 2)
13 | Label(root, text = 'Blue', background = 'Blue').grid(row = 1, column = 0, columnspan = 2)
14 |
15 | # root.rowconfigure(0, weight = 1)
16 | # root.rowconfigure(1, weight = 3)
17 | # root.columnconfigure(2, weight = 1)
18 |
19 | root.mainloop()
20 |
--------------------------------------------------------------------------------
/exercises/Module 4/pack.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 |
5 | Label(root, text = 'Hello, Tkinter!',
6 | background = 'yellow').pack(side = LEFT, anchor = 'nw')
7 | Label(root, text = 'Hello, Tkinter!',
8 | background = 'blue').pack(side = LEFT, padx = 10, pady = 10)
9 | Label(root, text = 'Hello, Tkinter!',
10 | background = 'green').pack(side = LEFT, ipadx = 10, ipady = 10)
11 |
12 |
13 | root.mainloop()
14 |
--------------------------------------------------------------------------------
/exercises/Module 4/place.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 | root.geometry('640x480+200+200')
5 |
6 | Label(root, text = 'Yellow', background = 'yellow').place(x = 100, y = 5)
7 | Label(root, text = 'Blue', background = 'blue').place(relx = 0.5, rely = 0.5,anchor='center')
8 | Label(root, text = 'Green', background = 'green').place(relx = 0.5, x = 100, rely=0.5,y=50)
9 | Label(root, text = 'Orange', background = 'orange').place(relx = 1.0, x = -5, y = 5,anchor='ne')
10 |
11 | # Label(root, text = 'Yellow', background = 'yellow').place(x = 100, y = 50, width = 100, height = 50)
12 | # Label(root, text = 'Blue', background = 'blue').place(relx = 0.5, rely = 0.5, anchor = 'center', relwidth = 0.5, relheight = 0.5)
13 | # Label(root, text = 'Green', background = 'green').place(relx = 0.5, x = 100, rely = 0.5, y = 50)
14 | # Label(root, text = 'Orange', background = 'orange').place(relx = 1.0, x = -5, y = 5, anchor = 'ne')
15 |
16 | root.mainloop()
17 |
--------------------------------------------------------------------------------
/exercises/Module 5/06_canvas.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 |
5 | c = Canvas(root)
6 | c.pack()
7 | c.config(width = 640, height = 480)
8 |
9 | #line = c.create_line(160, 360, 480, 120, fill = 'blue', width = 5)
10 | # c.itemconfigure(line, fill = 'green')
11 | # print(c.coords(line))
12 | # c.coords(line, 0, 0, 320, 240, 640, 0)
13 |
14 | # c.itemconfigure(line, smooth = True)
15 | # c.itemconfigure(line, splinesteps = 5)
16 | # c.itemconfigure(line, splinesteps = 100)
17 | # c.delete(line)
18 |
19 | # rect = c.create_rectangle(160, 120, 480, 360)
20 | # c.itemconfigure(rect, fill = 'yellow')
21 | #oval = c.create_oval(160, 120, 480, 360)
22 | # arc = c.create_arc(160, 1, 480, 240)
23 | # c.itemconfigure(arc, start = 0, extent = 180, fill = 'green')
24 | #poly = c.create_polygon(160, 360, 320, 480, 480, 360, fill = 'blue')
25 | text = c.create_text(320, 240, text = 'Python', font = ('Courier', 32, 'bold'))
26 |
27 | # logo = PhotoImage(file = 'python_logo.gif') # Change path as needed
28 | # image = c.create_image(320, 240, image = logo)
29 |
30 | # c.lift(text)
31 | # c.lower(image)
32 | # c.lower(image, text)
33 |
34 | button = Button(c, text = 'Click Me')
35 | c.create_window(320, 60, window = button)
36 |
37 | # canvas.itemconfigure(rect, tags = ('shape'))
38 | # canvas.itemconfigure(oval, tags = ('shape', 'round'))
39 | # canvas.itemconfigure('shape', fill = 'grey')
40 | # print(canvas.gettags(oval))
41 |
42 | root.mainloop()
43 |
--------------------------------------------------------------------------------
/exercises/Module 6/callback.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | def callback():
4 | print(1)
5 |
6 | # def callback(x):
7 | # print(x)
8 |
9 | root = Tk()
10 |
11 | Button(root, text = 'Click Me 1', command = callback()).pack()
12 |
13 | # Button(root, text = 'Click Me 1', command = lambda: callback(1)).pack()
14 | # Button(root, text = 'Click Me 2', command = lambda: callback(2)).pack()
15 | # Button(root, text = 'Click Me 3', command = lambda: callback(3)).pack()
16 |
17 |
18 | # Button(root, text = 'Click Me 1', command = lambda: print(1)).pack()
19 | # Button(root, text = 'Click Me 2', command = lambda: print(2)).pack()
20 | # Button(root, text = 'Click Me 3', command = lambda: print(3)).pack()
21 |
22 |
23 | root.mainloop()
24 |
25 |
26 |
--------------------------------------------------------------------------------
/exercises/Module 6/event-test.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | def update():
4 | label.config(text = 'Paris')
5 |
6 |
7 | root = Tk()
8 | label = Label(root, text = 'Answer')
9 | label.pack()
10 |
11 | Button(root, text = "Capital of France", command = update).pack()
12 | root.mainloop()
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/exercises/Module 6/keyboard.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | def key_press(event):
4 | print(event.char)
5 |
6 | root = Tk()
7 |
8 | root.bind('',key_press)
9 |
10 | root.mainloop()
11 |
--------------------------------------------------------------------------------
/exercises/Module 6/mouse.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | root = Tk()
3 |
4 | # def mouse_press(event):
5 | # print("Hello")
6 | # root.bind('', mouse_press)
7 |
8 | def click(event):
9 | print("clicked at", event.x, event.y)
10 |
11 | # frame = Frame(root, width=100, height=100)
12 | # frame.bind("", click)
13 | # frame.pack()
14 |
15 | root.mainloop()
16 |
--------------------------------------------------------------------------------
/exercises/Module 6/mouse_challenge.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | def mouse_press(event):
4 | global prev
5 | prev = event
6 |
7 | def draw(event):
8 | global prev
9 | canvas.create_line(prev.x, prev.y, event.x, event.y, width = 5)
10 | prev = event
11 |
12 | root = Tk()
13 |
14 | canvas = Canvas(root, width = 640, height = 480,
15 | background = 'white')
16 | canvas.pack()
17 | canvas.bind('', mouse_press)
18 | canvas.bind('', draw)
19 |
20 | root.mainloop()
21 |
--------------------------------------------------------------------------------
/exercises/Module 6/multiple.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 |
5 | label = Label(root, text = 'Label 1')
6 | label.pack()
7 |
8 | label.bind('', lambda e: print('Button Press'))
9 | label.bind('<2>', lambda e: print('Mouse Press'))
10 |
11 | root.mainloop()
12 |
--------------------------------------------------------------------------------
/exercises/Module 6/virtual.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | root = Tk()
4 |
5 | entry = Entry(root)
6 | entry.pack()
7 |
8 | entry.event_add('<>', '1', '3', '5', '7', '9')
9 | entry.bind('<>', lambda e: print('Odd Number!'))
10 |
11 | root.mainloop()
12 |
--------------------------------------------------------------------------------
/exercises/Module 7/app.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | class HelloApp:
4 |
5 | def __init__(self, master):
6 |
7 | self.label = Label(master, text = "Hello, Tkinter!")
8 | self.label.grid(row = 0, column = 0, columnspan = 2)
9 |
10 | Button(master, text = "Texas",
11 | command = self.texas_hello).grid(row = 1, column = 0)
12 |
13 | Button(master, text = "Hawaii",
14 | command = self.hawaii_hello).grid(row = 1, column = 1)
15 |
16 | def texas_hello(self):
17 | self.label.config(text = 'Howdy, Tkinter!')
18 |
19 | def hawaii_hello(self):
20 | self.label.config(text = 'Aloha, Tkinter!')
21 |
22 |
23 | def main():
24 |
25 | root = Tk()
26 | app = HelloApp(root)
27 | root.mainloop()
28 |
29 | if __name__ == "__main__": main()
30 |
--------------------------------------------------------------------------------
/exercises/Module 7/feedback_solution.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import messagebox
3 |
4 | class Feedback:
5 |
6 | def __init__(self, master):
7 |
8 | # master.title('Explore California Feedback')
9 | # master.resizable(False, False)
10 | # master.configure(background = '#e1d8b9')
11 |
12 | #self.style = Style()
13 | # self.style.configure('TFrame', background = '#e1d8b9')
14 | # self.style.configure('TButton', background = '#e1d8b9')
15 | # self.style.configure('TLabel', background = '#e1d8b9', font = ('Arial', 11))
16 | # self.style.configure('Header.TLabel', font = ('Arial', 18, 'bold'))
17 |
18 | self.frame_header = Frame(master)
19 | self.frame_header.pack()
20 |
21 | self.logo = PhotoImage(file = 'tour_logo.gif')
22 | Label(self.frame_header, image = self.logo).grid(row = 0, column = 0, rowspan = 2)
23 | Label(self.frame_header, text = 'Thanks for Exploring!', style = 'Header.TLabel').grid(row = 0, column = 1)
24 | Label(self.frame_header, wraplength = 300,
25 | text = ("We're glad you chose Explore California for your recent adventure. "
26 | "Please tell us what you thought about the 'Desert to Sea' tour.")).grid(row = 1, column = 1)
27 |
28 | self.frame_content = Frame(master)
29 | self.frame_content.pack()
30 |
31 | Label(self.frame_content, text = 'Name:').grid(row = 0, column = 0, padx = 5, sticky = 'sw')
32 | Label(self.frame_content, text = 'Email:').grid(row = 0, column = 1, padx = 5, sticky = 'sw')
33 | Label(self.frame_content, text = 'Comments:').grid(row = 2, column = 0, padx = 5, sticky = 'sw')
34 |
35 | self.entry_name = Entry(self.frame_content, width = 24, font = ('Arial', 10))
36 | self.entry_email = Entry(self.frame_content, width = 24, font = ('Arial', 10))
37 | self.text_comments = Text(self.frame_content, width = 50, height = 10, font = ('Arial', 10))
38 |
39 | self.entry_name.grid(row = 1, column = 0, padx = 5)
40 | self.entry_email.grid(row = 1, column = 1, padx = 5)
41 | self.text_comments.grid(row = 3, column = 0, columnspan = 2, padx = 5)
42 |
43 | Button(self.frame_content, text = 'Submit',
44 | command = self.submit).grid(row = 4, column = 0, padx = 5, pady = 5, sticky = 'e')
45 | Button(self.frame_content, text = 'Clear',
46 | command = self.clear).grid(row = 4, column = 1, padx = 5, pady = 5, sticky = 'w')
47 |
48 | def submit(self):
49 | print('Name: {}'.format(self.entry_name.get()))
50 | print('Email: {}'.format(self.entry_email.get()))
51 | print('Comments: {}'.format(self.text_comments.get(1.0, 'end')))
52 | self.clear()
53 | messagebox.showinfo(title = 'Explore California Feedback', message = 'Comments Submitted!')
54 |
55 | def clear(self):
56 | self.entry_name.delete(0, 'end')
57 | self.entry_email.delete(0, 'end')
58 | self.text_comments.delete(1.0, 'end')
59 |
60 | def main():
61 |
62 | root = Tk()
63 | feedback = Feedback(root)
64 | root.mainloop()
65 |
66 | if __name__ == "__main__": main()
67 |
--------------------------------------------------------------------------------
/exercises/Module 7/feedback_template.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | class Feedback:
4 |
5 | def __init__(self, master):
6 | pass
7 |
8 |
9 | def main():
10 |
11 | root = Tk()
12 | feedback = Feedback(root)
13 | root.mainloop()
14 |
15 | if __name__ == "__main__": main()
16 |
--------------------------------------------------------------------------------
/exercises/Module 7/feedback_template_with_events.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import messagebox
3 | from tkinter import ttk
4 |
5 | class Feedback:
6 |
7 | def __init__(self, master):
8 |
9 | master.title('Explore California Feedback')
10 | master.resizable(False, False)
11 | master.configure(background = '#e1d8b9')
12 |
13 | self.style = ttk.Style()
14 | self.style.configure('TFrame', background = '#e1d8b9')
15 | self.style.configure('TButton', background = '#e1d8b9')
16 | self.style.configure('TLabel', background = '#e1d8b9', font = ('Arial', 11))
17 | self.style.configure('Header.TLabel', font = ('Arial', 18, 'bold'))
18 |
19 |
20 | self.frame_header = Frame(master)
21 | self.frame_header.pack()
22 |
23 | self.logo = PhotoImage(file = '../images/tour_logo.gif')
24 | Label(self.frame_header, image = self.logo).grid(row = 0, column = 0, rowspan = 2)
25 | Label(self.frame_header, text = 'Thanks for Exploring!').grid(row = 0, column = 1)
26 | Label(self.frame_header, wraplength = 300,
27 | text = ("We're glad you chose Explore California for your recent adventure. "
28 | "Please tell us what you thought about the 'Desert to Sea' tour.")).grid(row = 1, column = 1)
29 |
30 | self.frame_content = Frame(master)
31 | self.frame_content.pack()
32 |
33 | Label(self.frame_content, text = 'Name:').grid(row = 0, column = 0, padx = 5, sticky = 'sw')
34 | Label(self.frame_content, text = 'Email:').grid(row = 0, column = 1, padx = 5, sticky = 'sw')
35 | Label(self.frame_content, text = 'Comments:').grid(row = 2, column = 0, padx = 5, sticky = 'sw')
36 |
37 | self.entry_name = Entry(self.frame_content, width = 24)
38 | self.entry_email = Entry(self.frame_content, width = 24)
39 | self.text_comments = Text(self.frame_content, width = 50, height = 10)
40 |
41 | self.entry_name.grid(row = 1, column = 0, padx = 5)
42 | self.entry_email.grid(row = 1, column = 1, padx = 5)
43 | self.text_comments.grid(row = 3, column = 0, columnspan = 2, padx = 5)
44 |
45 | Button(self.frame_content, text = 'Submit', command = self.submit).grid(row = 4, column = 0, padx = 5, pady = 5, sticky = 'e')
46 | Button(self.frame_content, text = 'Clear', command = self.clear).grid(row = 4, column = 1, padx = 5, pady = 5, sticky = 'w')
47 |
48 | def submit(self):
49 | print('Name: {}'.format(self.entry_name.get()))
50 | print('Email: {}'.format(self.entry_email.get()))
51 | print('Comments: {}'.format(self.text_comments.get(1.0, 'end')))
52 | self.clear()
53 | messagebox.showinfo(title = 'Feedback', message = 'Comments Submitted!')
54 |
55 | def clear(self):
56 | self.entry_name.delete(0, 'end')
57 | self.entry_email.delete(0, 'end')
58 | self.text_comments.delete(1.0, 'end')
59 |
60 | def main():
61 |
62 | root = Tk()
63 | app = Feedback(root)
64 | root.mainloop()
65 |
66 | if __name__ == "__main__": main()
--------------------------------------------------------------------------------
/exercises/Module 7/feedback_template_with_layout.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 | class Feedback:
4 |
5 | def __init__(self, master):
6 |
7 | self.frame_header = Frame(master)
8 | self.frame_header.pack()
9 |
10 | self.logo = PhotoImage(file = 'tour_logo.gif')
11 | Label(self.frame_header, image = self.logo).grid(row = 0, column = 0, rowspan = 2)
12 | Label(self.frame_header, text = 'Thanks for Exploring!').grid(row = 0, column = 1)
13 | Label(self.frame_header, wraplength = 300,
14 | text = ("We're glad you chose Explore California for your recent adventure. "
15 | "Please tell us what you thought about the 'Desert to Sea' tour.")).grid(row = 1, column = 1)
16 |
17 | self.frame_content = Frame(master)
18 | self.frame_content.pack()
19 |
20 | Label(self.frame_content, text = 'Name:').grid(row = 0, column = 0, padx = 5, sticky = 'sw')
21 | Label(self.frame_content, text = 'Email:').grid(row = 0, column = 1, padx = 5, sticky = 'sw')
22 | Label(self.frame_content, text = 'Comments:').grid(row = 2, column = 0, padx = 5, sticky = 'sw')
23 |
24 | self.entry_name = Entry(self.frame_content, width = 24)
25 | self.entry_email = Entry(self.frame_content, width = 24)
26 | self.text_comments = Text(self.frame_content, width = 50, height = 10)
27 |
28 | self.entry_name.grid(row = 1, column = 0, padx = 5)
29 | self.entry_email.grid(row = 1, column = 1, padx = 5)
30 | self.text_comments.grid(row = 3, column = 0, columnspan = 2, padx = 5)
31 |
32 | Button(self.frame_content, text = 'Submit').grid(row = 4, column = 0, padx = 5, pady = 5, sticky = 'e')
33 | Button(self.frame_content, text = 'Clear').grid(row = 4, column = 1, padx = 5, pady = 5, sticky = 'w')
34 |
35 | def main():
36 |
37 | root = Tk()
38 | feedback = Feedback(root)
39 | root.mainloop()
40 |
41 | if __name__ == "__main__": main()
42 |
--------------------------------------------------------------------------------
/exercises/Module 7/feedback_template_with_widgets.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 |
3 |
4 | class Feedback:
5 |
6 | def __init__(self, master):
7 |
8 | self.frame_header = Frame(master)
9 |
10 | self.logo = PhotoImage(file = '../images/tour_logo.gif')
11 | Label(self.frame_header, image = self.logo)
12 | Label(self.frame_header, text = 'Thanks for Exploring!')
13 | Label(self.frame_header, text = ("We're glad you chose Explore Californiafor your recent adventure. "
14 | "Please tell us what you thought about the 'Desert to Sea' tour."))
15 |
16 | self.frame_content = Frame(master)
17 | self.frame_content.pack()
18 |
19 | Label(self.frame_content, text = 'Name:').grid(row = 0, column = 0, padx = 5, sticky = 'sw')
20 | Label(self.frame_content, text = 'Email:').grid(row = 0, column = 1, padx = 5, sticky = 'sw')
21 | Label(self.frame_content, text = 'Comments:').grid(row = 2, column = 0, padx = 5, sticky = 'sw')
22 |
23 | self.entry_name = Entry(self.frame_content, width = 24)
24 | self.entry_email = Entry(self.frame_content, width = 24)
25 | self.text_comments = Text(self.frame_content, width = 50, height = 10)
26 |
27 | Button(self.frame_content, text = 'Submit')
28 | Button(self.frame_content, text = 'Clear')
29 |
30 | def main():
31 |
32 | root = Tk()
33 | app = Feedback(root)
34 | root.mainloop()
35 |
36 | if __name__ == "__main__": main()
--------------------------------------------------------------------------------
/exercises/images/0922.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from tkinter import ttk
3 |
4 | root = Tk()
5 |
6 | # Module 7 : Demo
7 |
8 |
9 |
10 | # Module 6 Events Handling
11 |
12 | # Multiple Events
13 | # w = Label(root, text="Label 1")
14 | # w.pack()
15 |
16 | # w.bind('', lambda e:print('Button Press'))
17 | # w.bind('<2>',lambda e: print("Press 2"))
18 | # root.mainloop()
19 |
20 | # Virtual Event
21 |
22 | # def printOdd(event):
23 | # print('Odd Number!')
24 |
25 | # entry = Entry(root)
26 | # entry.pack()
27 | # entry.event_add('<>','1','3','5','7','9')
28 | # entry.bind('<>',printOdd)
29 | # root.mainloop()
30 |
31 | # Binding to Mouse
32 |
33 | # def mouse_press(event):
34 | # print("x:{}, y:{}".format(event.x,event.y))
35 |
36 | # root.bind('',mouse_press)
37 |
38 | # Binding to Keyboard
39 | # def keyprint(event):
40 | # print('event.char')
41 | # def printCopy(event):
42 | # print('Copy')
43 | # def printPaste(event):
44 | # print('Paste')
45 |
46 | #root.bind('',keyprint)
47 | # root.bind('',printCopy)
48 | # root.bind('',printPaste)
49 |
50 |
51 | # Callback Commands
52 | # a = lambda x: x*x
53 | # print(a(5))
54 |
55 | # def callback(num):
56 | # print(num)
57 |
58 | # Button(root, text = 'Click Me 1', command = lambda: callback(1)).pack()
59 | # Button(root, text = 'Click Me 2', command = lambda: callback(2)).pack()
60 | # Button(root, text = 'Click Me 3', command = lambda: callback(3)).pack()
61 | # root.mainloop()
62 |
63 | # Module 5: Shapes
64 |
65 |
66 | # c = Canvas(root)
67 | # c.pack()
68 | # c.config(width=600,height=600)
69 | # b = Button(c, text = 'Click Me')
70 | # c.create_window(320, 60, window = b)
71 |
72 | # poly = c.create_polygon(160,360,320,480,480,360,fill='blue')
73 | # text = c.create_text(200,200,text='polygon',font=('Courier',32,'bold'))
74 | # arc = c.create_arc(50,50,200,200)
75 | # c.itemconfigure(arc, start=0, extent= 180, fill='red',width=5)
76 |
77 | # oval = c.create_oval(150,150,50,100)
78 | # c.itemconfigure(oval,fill='red')
79 | # rect = c.create_rectangle(50,50,150,150)
80 | # c.itemconfigure(rect,fill='blue')
81 |
82 | # line= c.create_line(10,10,50,50,fill='blue',width=5)
83 | # c.itemconfigure(line,fill='green')
84 | # c.coords(line,50,50,100,0)
85 | # Module 4: Geometry Management
86 |
87 | # Place
88 |
89 | # f1 = ttk.Frame(root, width=200,height=400,relief=RIDGE).grid(row=0,column=0,columnspan=2)
90 | # f2 = ttk.Frame(root, width=400,height=200,relief=RIDGE).grid(row=0,column=1)
91 | # f3 = ttk.Frame(root, width=400,height=200,relief=RIDGE).grid(row=1,column=2)
92 | # Label(f1, text = 'Nav').pack()
93 | # Label(f2, text = 'Text').pack()
94 | # Label(f3, text = 'Control').pack()
95 |
96 |
97 |
98 | # Label(f, text = 'Hello',background = 'green').place(relx=0.5,rely=0.25)
99 | # Label(f, text = 'Hello',background = 'orange').place(relx=0.25,rely=0.5,anchor='center')
100 | # Label(f, text = 'Hello',background = 'yellow').place(relx=0.75,rely=0.5)
101 | # Label(f, text = 'Hello',background = 'blue').place(relx=0.5,rely=0.75,anchor='ne')
102 |
103 |
104 | # Grid
105 | # Label(root, text = 'Hello',background = 'green').grid(row=1,column=1)
106 | # Label(root, text = 'Hello',background = 'orange').grid(row=1,column=0)
107 | # Label(root, text = 'Hello',background = 'yellow').grid(row=0,column=0,columnspan=2)
108 | # Label(root, text = 'Hello',background = 'blue').grid(row=2,column=0,columnspan=2)
109 |
110 | # Label(root, text = 'Hello',background = 'green').grid(row=0,column=0)
111 | # Label(root, text = 'Hello',background = 'orange').grid(row=0,column=1)
112 | # Label(root, text = 'Hello',background = 'yellow').grid(row=0,column=2,rowspan=2)
113 | # Label(root, text = 'Hello',background = 'blue').grid(row=1,column=0,columnspan=2)
114 |
115 | # Pack
116 | # w = Frame(root,width=100,height=100).pack()
117 |
118 | # Label(w, text = 'Hello',
119 | # background = 'yellow').pack(side=LEFT,padx=10,pady=10)
120 | # Label(w, text = 'Hello',
121 | # background = 'blue').pack(side=LEFT,ipadx=10,ipady=10)
122 | # Label(w, text = 'Hello',
123 | # background = 'green').pack(side=LEFT)
124 |
125 |
126 |
127 | # Module 3: Organizing Widgets
128 |
129 | # Notebook
130 |
131 | # w = ttk.Notebook(root)
132 | # w.pack()
133 | # f1 = ttk.Frame(w,width=50,height=50)
134 | # f2 = ttk.Frame(w,width=100,height=200)
135 | # w.add(f1,text='One')
136 | # #Text(f1,width=200,height=200).pack()
137 | # Label(f1,text="Hello").pack()
138 | # w.add(f2,text='Two')
139 | # Text(f2,width=100,height=100).pack()
140 |
141 | # Paned Window
142 |
143 | # w = ttk.PanedWindow(orient = HORIZONTAL)
144 | # w.pack(fill = BOTH, expand = True)
145 |
146 | # f1 = ttk.Frame(w, width = 100, height = 300, relief = SUNKEN)
147 | # f2 = ttk.Frame(w, width = 200, height = 400, relief = SUNKEN)
148 | # w.add(f1,weight=1)
149 | # w.add(f2,weight=2)
150 |
151 | # f3 = ttk.Frame(w, width = 300, height = 50, relief = SUNKEN)
152 | # Label(f3,text="Frame3").pack()
153 | # w.insert(2, f3)
154 | #w.add(f3,weight=3)
155 |
156 | # top = Label(f1, text="top pane")
157 | # top.pack()
158 |
159 | # Windows
160 |
161 | # w1 = Toplevel(root)
162 | # w1.title('Window 1')
163 | # w1.geometry('200x100+50+50')
164 | # w1.resizable(True,True)
165 | # w1.maxsize(300,300)
166 |
167 | # w2 = Toplevel(root)
168 | # w2.title('Window 2')
169 | # w2.geometry('300x300+100+100')
170 | # w2.destroy()
171 |
172 |
173 | # w1.lift(w2)
174 | # w2.lift(root)
175 | # w1.lower(w2)
176 |
177 |
178 | # Frames
179 | # f1 = ttk.Frame(root)
180 | # f1.pack()
181 | # f1.config(height=100,width=200)
182 | # f1.config(relief=RIDGE)
183 |
184 | # Label(f1, text="Hello on Frame 1").pack(side=LEFT)
185 | # Button(f1, text = 'Click Me on Frame 1').pack(side=RIGHT)
186 |
187 |
188 | # f2 = ttk.Frame(root)
189 | # f2.pack()
190 | # f2.config(height=300,width=400)
191 | # f2.config(relief=SUNKEN)
192 |
193 | # Label(f2, text="Hello on Frame 2").pack(side=LEFT)
194 | # Button(f2, text = 'Click Me on Frame 2').pack(side=RIGHT)
195 |
196 |
197 |
198 |
199 | # Syling
200 |
201 | # style = ttk.Style()
202 | # style.theme_use('default')
203 |
204 | # def callback():
205 | # label.config(text='Paris!')
206 |
207 | # label = Label(root, text="What is the Capital of France!")
208 | # label.pack()
209 | # button = Button(root)
210 | # button.pack()
211 | # button.config(text='Show Answer',command=callback)
212 |
213 | #Scrollbar
214 |
215 | # sx = ttk.Scrollbar(root, orient = HORIZONTAL)
216 | # sy = ttk.Scrollbar(root, orient = VERTICAL)
217 | # sx.pack(side=BOTTOM,fill=X)
218 | # sy.pack(side=RIGHT,fill=Y)
219 |
220 | # text = Text(root, width=40, height=20, xscrollcommand=sx.set, yscrollcommand=sy.set)
221 | # for i in range(1000):
222 | # text.insert(END, str(i))
223 | # text.pack(side=LEFT, fill=BOTH)
224 | # text.config(wrap="none")
225 | # sy.config(command=text.yview)
226 | # sx.config(command=text.xview)
227 |
228 | # Menu
229 | #root.option_add('*tearOff', False)
230 | # menubar = Menu(root)
231 | # root.config(menu = menubar)
232 | # file = Menu(menubar)
233 | # edit = Menu(menubar)
234 | # help_ = Menu(menubar)
235 |
236 | # Module 2 Widgets
237 |
238 | # Treeview (List)
239 | # w = ttk.Treeview(root)
240 | # w.pack()
241 | # w.insert('', '0', 'item1', text = 'First Item')
242 | # w.insert('', '1', 'item2', text = 'Second Item')
243 | # w.insert('', 'end', 'item3', text = 'Third Item')
244 | # w.insert('', 'end', 'item4', text = 'Fourth Item')
245 | # w.move('item2','item1','end')
246 | # w.move('item3','item1','0')
247 | # w.move('item4','item3','end')
248 | # w.item('item1',open=True)
249 | # w.item('item3',open=True)
250 | #w.detach('item4')
251 | #w.delete('item4')
252 |
253 | # Scale Bar
254 | # w = Scale(root,from_= 0,to = 100, orient = HORIZONTAL)
255 | # w.pack()
256 |
257 | # Progress Bar
258 | # w = ttk.Progressbar(root, orient = HORIZONTAL, length = 200)
259 | # w.pack()
260 | # w.config(mode = 'indeterminate')
261 | # w.start()
262 | # w.stop()
263 |
264 | #w.config(mode = 'determinate', maximum = 11.0, value = 2)
265 | # w.config(value = 8.0)
266 | # w.step()
267 | # w.step()
268 | # w.step()
269 | # w.step()
270 | # w.step(5)
271 |
272 |
273 | # Spin Box
274 | # year = StringVar()
275 | # Spinbox(root, from_ = 1990, to = 2014, textvariable = year).pack()
276 |
277 | # root.mainloop()
278 | # print(year.get())
279 |
280 | #Combo Box
281 | # month = StringVar()
282 | # combobox = ttk.Combobox(root,textvariable=month)
283 | # combobox.pack()
284 | # combobox.config(values = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
285 | # 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
286 | # month.set('Dec')
287 | # root.mainloop()
288 | # print(month.get())
289 | # Text
290 |
291 | # s = StringVar()
292 | # w = Text(root,width=20,height=5)
293 | # w.pack()
294 | # w.config(wrap='word')
295 | # w.insert('2.0','1111\n2222\n3333')
296 | # w.delete('2.0','2.end')
297 | # # s = w.get('1.0','1.end')
298 | # root.mainloop()
299 | # print(s)
300 |
301 | #Entry
302 | # s = StringVar()
303 | # w = Entry(root,width=30,textvariable=s)
304 | # w.pack()
305 | # w.insert(0,'Enter your password')
306 | # w.config(show="*")
307 | # w.delete(0,END)
308 | # w.config(state='disabled')
309 | # w.config(state='normal')
310 | # root.mainloop()
311 | # print(s.get())
312 |
313 | # Radio Button
314 | # MODES = [ ("Monochrome", "M"), ("Grayscale", "G"), ("True color", "RGB"), ("Color separation", "CMYK")]
315 | # v = StringVar()
316 | # def showMode():
317 | # print(v.get())
318 | # for text, mode in MODES:
319 | # Radiobutton(root, text=text, variable=v, value=mode, command=showMode).pack(anchor='w')
320 | # a = IntVar()
321 | # def showInt():
322 | # print(a.get())
323 | # Radiobutton(root,text='1',value='1',variable=a,command=showInt).pack()
324 | # Radiobutton(root,text='2',value='2',variable=a,command=showInt).pack()
325 | #Radiobutton(root,text='3',value='1').pack()
326 | # root.mainloop()
327 | #print(a.get())
328 |
329 | # Check Button
330 | # s = StringVar()
331 | # s.set('No')
332 | # w = ttk.Checkbutton(root,text='Hungry?')
333 | # w.pack()
334 | # w.config(variable=s,onvalue="Yes",offvalue="No")
335 |
336 | # root.mainloop()
337 | # print(s.get())
338 |
339 | # a = IntVar()
340 | # a.set(5)
341 | # print(a.get())
342 | # Button
343 | # def callback():
344 | # label.config(text='Paris!')
345 |
346 | # label = Label(root, text="What is the Capital of France!")
347 | # label.pack()
348 | # button = Button(root)
349 | # button.pack()
350 | # button.config(text='Show Answer',command=callback)
351 |
352 | # def callback():
353 | # print('Thanks!')
354 |
355 | # w = Button(root)
356 | # w.pack()
357 | # w.config(text='Click Me',command=callback)
358 | # w.config(state='disabled')
359 | # w.config(state='active')
360 | # logo = PhotoImage(file='../images/python_logo.gif')
361 | # small_logo = logo.subsample(10,10)
362 | # w.config(image=small_logo,compound=LEFT)
363 |
364 | #w['text']='Click Me'
365 |
366 | # Label
367 | #w = Label(root, text="Hello!",foregroun='blue',background='yellow')
368 | # w = Label(root, text="Hello!")
369 | # w.pack()
370 | # logo = PhotoImage(file='../images/python_logo.gif')
371 | # w.config(image=logo)
372 | # w.config(foreground='blue')
373 | # w.config(background='yellow')
374 | # w.config(font=('Courier',18,'bold'))
375 | # w.config(wraplength = 200)
376 |
377 | # def doSomething():
378 | # print("Hello!")
379 |
380 | #Label(root, text="Hello!").pack()
381 | #Button(root, text="Click Me", command=doSomething).pack()
382 |
383 |
384 | #root.mainloop()
--------------------------------------------------------------------------------
/exercises/images/python_logo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tertiarycourses/TkinterTraining/904f82e6b04753aba6a58f3299dbac5ec4be386a/exercises/images/python_logo.gif
--------------------------------------------------------------------------------
/exercises/images/tour_logo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tertiarycourses/TkinterTraining/904f82e6b04753aba6a58f3299dbac5ec4be386a/exercises/images/tour_logo.gif
--------------------------------------------------------------------------------