Tkinter and threading
One of the simplest ways that we can make our root window responsive is to use the root.update()
method within our play_pattern
loop. This updates the root.mainloop()
method after each sound sample is played.
However, this is an inelegant method because the control is passed to the main loop with some staggering experienced in the GUI. Thus, you may experience a slight delay in the responses of other widgets in the Toplevel.
Further, if some other event causes the method to be called, it could result in a nested event loop.
A better solution would be to run the play_pattern
method from a separate thread. Let us use the threading module of Python to play the pattern in a separate thread. This way, pygame
will not interfere with Tkinter's main loop.
A thread is a coding construct that can advance two or more separate sets of logical workflow together within an instance of a running program (process), context-switching between the workflows. Each thread in a running program...