(The WxPython version of this can be found here)
If you have ever tried to use
You will notice that the call to change the label text to running doesn't seem work,the listbox does not update until after the sleep has finished, and the button locks in the down position.
Example of the problem
Example of adding a thread but still getting a error
To get around this locking up problem we can use threads.
If we use a thread on its own the gui will become responsive but if the button is pressed a few times in a row, the number sequence will get jumbled up because each time the button is pressed a new thread is started.
There will also be an error if the gui is closed while the threads are still working, the gui loop doesn't like seperate threads calling it.
Example of a solution to the problem
To get this working correctly we can use tk's after to make the gui changes happen in the gui thread and so only one thread is running and multiple clicks are queued up we can use concurrent futures ThreadPoolExecutor.
Example of a solution to the problem using decorators
Decorators of after call and ThreadPoolExecutor can be used as shown below, by using these any blocking method just needs to be decorated by
[Tkinter] Redirecting stdout to TextBox in realtime
Edit: improved
If you have ever tried to use
time.sleep
or any code that takes some time to run within your gui code, you will find it becomes unresponsive like in the following example.You will notice that the call to change the label text to running doesn't seem work,the listbox does not update until after the sleep has finished, and the button locks in the down position.
Example of the problem
import time import tkinter as tk class MainFrame(tk.Frame): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.label = tk.Label(self, text='not running') self.label.pack() self.listbox = tk.Listbox(self) self.listbox.pack() self.button = tk.Button( self, text='blocking task', command=self.on_button) self.button.pack(pady=15) self.pack() def on_button(self): print('Button clicked') self.blocking_code() def blocking_code(self): self.label['text'] = 'running' for number in range(5): self.listbox.insert(tk.END, number) print(number) time.sleep(1) self.label['text'] = 'not running' if __name__ == '__main__': app = tk.Tk() main_frame = MainFrame() app.mainloop()
Output:Button clicked
0
1
2
3
4
Example of adding a thread but still getting a error
To get around this locking up problem we can use threads.
If we use a thread on its own the gui will become responsive but if the button is pressed a few times in a row, the number sequence will get jumbled up because each time the button is pressed a new thread is started.
There will also be an error if the gui is closed while the threads are still working, the gui loop doesn't like seperate threads calling it.
Error:RuntimeError: main thread is not in main loop
import threading class MainFrame(tk.Frame): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) .... .... def on_button(self): print('clicked') thread = threading.Thread(target=self.blocking_code) thread.start() def blocking_code(self): self.label['text'] = 'running'
Output:clicked
Button clicked
0
Button clicked
0
1
1
2
Button clicked
0
2
3
1
Error:Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\Dave\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\Dave\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Dave\Documents\Eclipse Workspace\Test\forum\tkinter_blocking.py", line 119, in blocking_code
self.listbox.insert(tk.END, number)
File "C:\Users\Dave\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2806, in insert
self.tk.call((self._w, 'insert', index) + elements)
RuntimeError: main thread is not in main loop
Example of a solution to the problem
To get this working correctly we can use tk's after to make the gui changes happen in the gui thread and so only one thread is running and multiple clicks are queued up we can use concurrent futures ThreadPoolExecutor.
import tkinter as tk from concurrent import futures import time thread_pool_executor = futures.ThreadPoolExecutor(max_workers=1) class MainFrame(tk.Frame): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.label = tk.Label(self, text='not running') self.label.pack() self.listbox = tk.Listbox(self) self.listbox.pack() self.button = tk.Button( self, text='blocking task', command=self.on_button) self.button.pack(pady=15) self.pack() def on_button(self): print('Button clicked') thread_pool_executor.submit(self.blocking_code) def set_label_text(self, text=''): self.label['text'] = text def listbox_insert(self, item): self.listbox.insert(tk.END, item) def blocking_code(self): self.after(0, self.set_label_text, 'running') for number in range(5): self.after(0, self.listbox_insert, number) print(number) time.sleep(1) self.after(0, self.set_label_text, ' not running') if __name__ == '__main__': app = tk.Tk() main_frame = MainFrame() app.mainloop()
Output:Button clicked
0
Button clicked
Button clicked
1
2
3
4
0
1
2
3
4
0
1
2
3
4
Example of a solution to the problem using decorators
Decorators of after call and ThreadPoolExecutor can be used as shown below, by using these any blocking method just needs to be decorated by
submit_to_pool_executor
and any methods called from a separate thread just need decorating with tk_after
.import tkinter as tk from concurrent import futures import time import functools thread_pool_executor = futures.ThreadPoolExecutor(max_workers=1) def tk_after(target): @functools.wraps(target) def wrapper(self, *args, **kwargs): args = (self,) + args self.after(0, target, *args, **kwargs) return wrapper def submit_to_pool_executor(executor): '''Decorates a method to be sumbited to the passed in executor''' def decorator(target): @functools.wraps(target) def wrapper(*args, **kwargs): result = executor.submit(target, *args, **kwargs) result.add_done_callback(executor_done_call_back) return result return wrapper return decorator def executor_done_call_back(future): exception = future.exception() if exception: raise exception class MainFrame(tk.Frame): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.label = tk.Label(self, text='not running') self.label.pack() self.listbox = tk.Listbox(self) self.listbox.pack() self.button = tk.Button( self, text='blocking task', command=self.on_button) self.button.pack(pady=15) self.pack() def on_button(self): print('Button clicked') self.blocking_code() @tk_after def set_label_text(self, text=''): self.label['text'] = text @tk_after def listbox_insert(self, item): self.listbox.insert(tk.END, item) print(item) @submit_to_pool_executor(thread_pool_executor) def blocking_code(self): self.set_label_text('running') for number in range(5): self.listbox_insert(number) time.sleep(1) self.set_label_text('not running') if __name__ == '__main__': app = tk.Tk() main_frame = MainFrame() app.mainloop()There is an example here
[Tkinter] Redirecting stdout to TextBox in realtime
Edit: improved
submit_to_pool_executor
previously errors in the threaded code would happen silently, errors will now be raised by the call back executor_done_call_back