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 window.

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's use the threading module of Python to play the pattern in a separate thread. This way, pygame will not ...

Get Tkinter GUI Application Development Blueprints - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.