1
+ #basic window with widgets
2
+ '''
3
+ Widgets are building blocks of Tkinter
4
+ i.e. texts,buttons,menus,frames etc.
5
+ Understanding the widgets and controlling them is key to mastering any GUI framework
6
+
7
+ TKinter has 2 sets of widgets
8
+ tk widgets were the og
9
+ ttk widgets were added later on (more modern)
10
+ '''
11
+ import tkinter as tk
12
+ from tkinter import ttk
13
+
14
+ def button_func ():
15
+ print ('A button was clicked' )
16
+ def print_hello ():
17
+ print ('hello' )
18
+
19
+ #crete window - call tk.Tk() function & its gonna return the actual
20
+ # window in which we place everything in
21
+ window = tk .Tk ()
22
+ #to display a certain variable - variable.mathod()
23
+ window .title ('Window & Widgets' )
24
+ #set size of window geometry('widthxheight')
25
+ window .geometry ('800x500' )
26
+
27
+ #ttk widgets
28
+ label = ttk .Label (master = window , text = 'Thi is a test' )
29
+ label .pack ()
30
+
31
+ #tk.text
32
+ text = tk .Text (master = window )
33
+ text .pack ()
34
+ #tk.Text(master = window) only cretes the widget & tells what the parent is. Does not place it visually.
35
+ #the method .pack() takes a widget and then packs it all the way on the top center like a stack
36
+
37
+ #ttk entry
38
+ entry = ttk .Entry (master = window )
39
+ entry .pack ()
40
+
41
+ text_label = ttk .Label (master = window , text = 'My label' )
42
+ text_label .pack ()
43
+
44
+ #ttk button - never call a function in command as we don't want the button to call the function without getting clicked
45
+ button = ttk .Button (master = window , text = 'A Button' , command = button_func )
46
+ button .pack ()
47
+
48
+ #exercise
49
+ #text label
50
+
51
+ #button
52
+ #button_hello = ttk.Button(master=window, text='Print Hello', command=print_hello)
53
+ button_hello = ttk .Button (master = window , text = 'Print Hello' , command = lambda :print ('hello' ))
54
+ button_hello .pack ()
55
+
56
+ #run
57
+ window .mainloop ()
58
+ '''
59
+ mainloop - 1. Updates to GUI
60
+ [Wriiten text or updatation of widgets actually gets displayed]
61
+ 2. Checks for events [Button clicks, mouse movements, closing the window]
62
+ without mainloop app can't run
63
+ '''
0 commit comments