6
6
7
7
class App :
8
8
def __init__ (self ,root ,title )-> None :
9
+ # root is the window where all the widgits will be displayed
9
10
self .root = root
10
11
self .root .title (title )
11
12
self .root .geometry ('805x440' )
13
+ # create a button and tell it where to display the button i.e. self.root
12
14
ttk .Button (self .root ,text = 'start sorting' ,command = self ._start ).grid (row = 0 ,column = 14 )
15
+ # now to add this created widget (button) on the screen (window (self.root))
16
+ # use either grid or pack
13
17
ttk .Button (self .root ,text = 'shuffle' ,command = self ._shuffle ).grid (row = 0 ,column = 13 )
14
18
self .canvas = Canvas (self .root ,width = 800 ,height = 405 ,highlightbackground = 'dodgerblue' ,
15
19
bg = 'black' ,highlightthickness = 2 )
@@ -22,9 +26,11 @@ def __init__(self,root,title) -> None:
22
26
self ._shuffle ()
23
27
24
28
def __reset_colors (self ,color = 'dodgerblue' ):
29
+ # lets create a function to reset the colors
25
30
self ._colors = [color for _ in range (self .N )]
26
31
27
32
def _shuffle (self ):
33
+ # define a shuffle function
28
34
self .__reset_colors ()
29
35
random .shuffle (self .data )
30
36
self ._display (self ._colors )
@@ -73,5 +79,33 @@ def _start(self):
73
79
74
80
if __name__ == '__main__' :
75
81
window = Style (theme = 'darkly' ).master
82
+ # window is an instance of ttkbootstrap's Style class
76
83
App (window ,'Bubble sort' )
77
- window .mainloop ()
84
+
85
+ window .mainloop ()
86
+ # tells Python to run the Tkinter event loop.
87
+ # This method listens for events, such as button clicks or keypresses,
88
+ # and blocks any code that comes after it from running
89
+ '''
90
+ The mainloop() function in Tkinter is a method that starts an event loop,
91
+ which listens for events and responds to them accordingly.
92
+ It is a crucial function in GUI programming with Tkinter
93
+ since it is responsible for keeping the GUI responsive
94
+ and handling user input.
95
+
96
+ When you create a GUI application using Tkinter, you define
97
+ various widgets like buttons, labels, and entry boxes, and
98
+ then bind them to specific events like button clicks or key
99
+ presses. Once you have set up your application, you need
100
+ to start the mainloop() function to start the event loop.
101
+
102
+ The mainloop() function runs indefinitely until the user
103
+ closes the application window or calls the quit() method.
104
+ It continuously listens for events such as mouse clicks,
105
+ key presses, and other user input events, and dispatches
106
+ them to the appropriate widgets for processing.
107
+
108
+ In summary, the purpose of the mainloop() function in
109
+ Tkinter is to start the event loop that listens for
110
+ user input events and keeps the GUI responsive.
111
+ '''