How to do it...

Adding a loop into our button callback method with some sleep time results in our GUI becoming unresponsive and, when we try to close the GUI, things get even worse.

  1. Open GUI_multiple_threads.py and save it as GUI_multiple_threads_sleep_freeze.py.
  2. Make the following changes to the code:
# Button callbackdef click_me(self):     self.action.configure(text='Hello ' + self.name.get() + ' '     + self.number_chosen.get())    # Non-threaded code with sleep freezes the GUI    for idx in range(10):        sleep(5)        self.scrol.insert(tk.INSERT, str(idx) + 'n')
  1. Running the preceding code results in the following screenshot:
  1. Let's move the creation ...

Get Python GUI Programming Cookbook - Third 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.