Run thread from Tkinter and wait until it's finished - python

I have a tkinter application (runs as main thread), within it I open new top-level window - it is a log windows printing result of tests (the test are performed with selenium webdriver). This dialog is also a caller of all the tests.
So I want to display the dialog (as top-level, there is one more window for whole application), run a test, wait until the test is done and print the result, then do the same for another test unit. But I don't want to make the window freeze during the tests.
I've tried to use threads, but it obviously it can work just like that. In this case the dialog doesn't even start till tests are done.
Here's the code of the dialog window.
class TestDialog(tkinter.Toplevel):
def __init__(self, parent, tester, url):
super().__init__(parent)
self.parent = parent
self.webtester = tester;
self.__initComponents()
self.run(url)
self.wait_window(self)
def __initComponents(self):
self.transient(self.parent)
frame = tkinter.Frame(self)
self._tarea = tkinter.Text(frame, state='disabled',wrap='none', width=55, height=25)
vsb = tkinter.Scrollbar(frame, orient=tkinter.VERTICAL, command=self._tarea.yview)
self._tarea.configure(yscrollcommand=vsb.set)
self._tarea.grid(row=1, column=0, columnspan=4, sticky="NSEW", padx=3, pady=3)
vsb.grid(row=1, column=4, sticky='NS',pady=3)
frame.grid(row=0, column=0, sticky=tkinter.NSEW)
frame.columnconfigure(0, weight=2)
frame.rowconfigure(1, weight=1)
window = self.winfo_toplevel()
window.columnconfigure(0, weight=1)
window.rowconfigure(0, weight=1)
self.bind("<Escape>", self.close)
self.protocol("WM_DELETE_WINDOW", self.close)
self.grab_set()
def appendLine(self, msg):
self._tarea['state'] = 'normal'
self._tarea.insert("end", msg+'\n')
self._tarea['state'] = 'disabled'
def run(self, url):
self.appendLine("Runneing test #1...")
try:
thr = threading.Thread(target=self.webtester.urlopen, args=(url,))
thr.start()
except:
pass
thr.join()
self.webtester.urlopen(url)
self.appendLine("Running test #2")
try:
thr = threading.Thread(target=self.webtester.test2)
thr.start()
except:
pass
def close(self, event=None):
self.parent.setBackgroundScheme(DataTreeView.S_DEFAULT)
self.parent.focus_set()
self.destroy()
This dialog is opened from parent window simply by:
testDialog = TestDialog(self.parent, self._webtester, url)
Thank you for any advice.

To prevent the GUI from freezing you need self.run() to end quickly.
It needs to spawn a thread, start the thread, then end:
import Queue
sentinel = object()
root = tkinter.Tk()
...
def run(self, url):
outqueue = Queue.Queue()
thr = threading.Thread(target=self.run_tests, args=(url, outqueue))
thr.start()
root.after(250, self.update, outqueue)
Now the function this thread runs can run for a long time:
def run_tests(self, url, outqueue):
outqueue.put("Running test #1...")
self.webtester.urlopen(url)
outqueue.put("Running test #2")
self.webtester.test2()
outqueue.put(sentinel)
But because Tkinter expects all GUI calls to originate from a single thread, this spawned thread must not make any GUI calls. For it to interact with the GUI, you can send output (such as a status update message) through a Queue.Queue and concurrently let the main Tkinter thread monitor this Queue.Queue periodically (through calls to root.after):
def update(self, outqueue):
try:
msg = outqueue.get_nowait()
if msg is not sentinel:
self.appendLine(msg)
root.after(250, self.update, outqueue)
else:
# By not calling root.after here, we allow update to
# truly end
pass
except Queue.Empty:
root.after(250, self.update, outqueue)

Related

Use threading and queues to redirect logger messages to tkinter widget in python

greetings to the community.
I am facing the following problem: I have a simple tkinter GUI with a button that launches a number of calculations while printing in a tkinter widget various messages related to the calculation's progress.
I have read a few posts and, from what I understand, the most efficient way is to create a queue in the main thread, bind the queue to a logger, run the calculations in a separate thread, and redirect its messages to the queue which is polled by the main thread using an interval.
But I am clearly doing something wrong. The messages are written in my scrolled frame widget but all at once after the threaded operation finishes.
Here is my code (I will simplify it as best as I can):
class GFTGUI(ttk.Frame):
def __init__(self, parent):
ttk.Frame.__init__(self, parent)
self.app = parent
# create widget to store messages
logger_pane = ttk.PanedWindow(self.app, orient=VERTICAL)
logger_pane.grid(row=2, column=0, sticky=(N, W, E, S), padx=10, pady=10)
self.logger_frame = ttk.Labelframe(logger_pane, text="Message Board")
logger_pane.add(self.logger_frame, weight=1)
self.scrolled_text = ScrolledText(self.logger_frame, state='disabled')
self.scrolled_text.grid(row=0, column=0, sticky=(N, S, W, E))
self.scrolled_text.configure(font='TkFixedFont')
# configure GUI logger
ConfigureTkFrameLogger(self.logger_frame, self.scrolled_text, self.gft)
# create the run button
self.run_button = Button(self.gft, text="Run!", command=self.run_command, height=35)
self.run_button.grid(column=0, row=1, padx=3, pady=4)
def run_command(self):
th = threading.Thread(target=run_calculations) # see final block of code
th.start()
th.join()
def start_gui():
app = Tk()
GFTGUI(app)
app.mainloop()
if __name__ == '__main__': start_gui()
Then I have a separate file (called helpers.py) that stores the class for the logger:
logger_user = logging.getLogger('user')
class ConfigureTkFrameLogger:
def __init__(self, logger_frame, scrolled_text):
self.logger_frame = logger_frame
self.scrolled_text = scrolled_text
# Create a logging handler using a queue
self.log_queue = queue.Queue()
self.queue_handler = QueueHandler(self.log_queue)
formatter = logging.Formatter('%(asctime)s: %(message)s', datefmt='%H:%M:%S')
self.queue_handler.setFormatter(formatter)
self.queue_handler.set_name('gui')
logger_user.addHandler(self.queue_handler)
# Start polling messages from the queue
self.logger_frame.after(100, self.poll_log_queue)
# Check every 100ms if there is a new message in the queue to display
def poll_log_queue(self):
while True:
try: record = self.log_queue.get(block=False)
except queue.Empty: break
else: self.display(record)
self.logger_frame.after(100, self.poll_log_queue)
def display(self, record):
msg = self.queue_handler.format(record)
self.scrolled_text.configure(state='normal')
self.scrolled_text.insert(END, msg + '\n', record.levelname)
self.scrolled_text.configure(state='disabled')
self.scrolled_text.yview(END) # Autoscroll to the bottom
class QueueHandler(logging.Handler):
def __init__(self, log_queue):
super().__init__()
self.log_queue = log_queue
def emit(self, record): self.log_queue.put(record)
Finally, my calculations are done in a method stored in a separate file:
logger = logging.getLogger('user')
def run_calculations(some_args...):
# this message should be printed as soon as the calculations begin
logger.info('Reading input file')
do other stuff here
I think I am close in making it work. I just need a little push!
This would be a comment, but I "must have 50 reputation to comment", so.
Without looking at anything else, I see this:
th.start()
th.join()
Do not start & immediately join (== wait for thread to finish).

How to correctly implement multithreading using a Tkinter GUI?

I am trying to implement Multithreading while using a GUI in tkinter. I came along this solution, but i am failing to implement it.
So basically i need to know:
How do i need to alter my Code to make the Progressbar interact nice and smoothly, i.e. not going in unresponsive mode when the GUI loses focus?
This is my code boiled down:
from tkinter import *
import queue
import threading
from tkinter.ttk import *
class ThreadedTask(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
# Gui class
class MyGui(Frame):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.queue = queue.Queue()
self.init_ui()
# define a button to fulfill long task
def init_ui(self):
self.frame = Frame(self, relief=RAISED, borderwidth=1)
self.frame.grid(row=0, column=0, columnspan=1, sticky='ns')
self.status_frame = Frame(self, relief=RAISED, borderwidth=1, height=20)
self.status_frame.grid(row=1, column=0, columnspan=3, sticky='nesw')
self.status_frame.grid_configure(padx=3, pady=3)
self.button = Button(self.frame, text='do Stuff', command=self.do_stuff)
self.button.grid(padx=3, pady=3)
self.progress = Progressbar(self.status_frame, orient="horizontal", length=80, mode="determinate")
self.progress.grid(row=1, column=0, pady=3, sticky='nesw')
self.grid()
# start ThreadedTask here
def do_stuff(self):
ThreadedTask(self.queue).start()
self.queue_process(DoStuffClass(ui=self))
def queue_process(self, process, retry_time=10):
self.master.after(retry_time, process)
def update_status(self):
self.parent.update_idletasks()
class DoStuffClass:
def __init__(self, ui=None):
self.ui = ui
self.long_task()
def long_task(self):
# do stuff here and update the progressbar from MyGui
self.ui.progress['maximum'] = 10000
# some lengthy task ...
for i in range(10000):
print(i)
self.ui.progress.step()
self.ui.parent.update_idletasks()
# main
root = Tk()
root.geometry("150x80+50+50")
MyGui(root)
root.mainloop()
root.quit()
I think right now my problem is the wrong implementation of queues and Threading, i.e. self.queue_process(DoStuffClass(ui=self))... the behaviour is the same as if i wouldn't use a queue and Multithread at all. The progressbar works, as long as it stays "in focus", which means me not clicking anything else on the desktop. As soon as i'm clicking elsewhere on the desktop and the GUI loses focus, the GUI goes in "unresponsive" mode and the progress bar does not update anymore. Also, sometimes Tcl closes the wrong Thread, which makes the whole program crash.
So after a couple of tries i figured out what to do:
from tkinter import *
import queue
import threading
from tkinter.ttk import *
class ThreadedTask(object):
def __init__(self, parent):
self.parent = parent
self.queue = queue.Queue()
self.gui = MyGui(parent, self.queue)
self.work_queue()
def work_queue(self):
""" Check every 100 ms if there is something new in the queue. """
try:
self.parent.after(200, self.work_queue)
print('working queue with task {}...'.format(self.queue.get_nowait()))
except queue.Empty:
pass
# Gui class
class MyGui(Frame):
def __init__(self, parent, queue):
super().__init__(parent)
self.parent = parent
self.queue = queue
self.init_ui()
# define a button to fulfill long task
def init_ui(self):
self.frame = Frame(self, relief=RAISED, borderwidth=1)
self.frame.grid(row=0, column=0, columnspan=1, sticky='ns')
self.status_frame = Frame(self, relief=RAISED, borderwidth=1, height=20)
self.status_frame.grid(row=1, column=0, columnspan=3, sticky='nesw')
self.status_frame.grid_configure(padx=3, pady=3)
self.button = Button(self.frame, text='do Stuff', command=self.init_button_loop)
self.button.grid(padx=3, pady=3)
self.progress = Progressbar(self.status_frame, orient="horizontal", length=80, mode="determinate")
self.progress.grid(row=1, column=0, pady=3, sticky='nesw')
self.grid()
def start_thread(self, function_name, queue):
t = threading.Thread(target=function_name, args=(queue,))
# close thread automatically after finishing task
t.setDaemon(True)
t.start()
# execute button push by spawning a new thread
def init_button_loop(self):
self.start_thread(self.exec_button_loop, self.queue)
# execute long task
def exec_button_loop(self, queue):
self.progress['maximum'] = 10000
for i in range(10000):
# update progressbar
queue.put(self.progress.step())
# main
root = Tk()
root.geometry("150x80+50+50")
client = ThreadedTask(root)
root.mainloop()
The Difficulty was to figure out how to interact with queues and threads, while pressing buttons in the gui.
Basically my fault was to declare the queue in the wrong class and the wrong use of the after function while not knowing where to start the threads.
The new implementation follows the principle of filling the queue up in a new thread, which is spawned when pressing the gui button. The queue is periodically checked from the main thread whether theres something to do. This prevents unresponsiveness due to a safe communication between gui and mainthread.

Starting and stopping a thread from a gui

I'm currently developing a program with a GUI to gather data from a sensor and visualise it within the GUI.
The data from the sensor is stored in a list for further calculations.
What I'm currently trying to do, is starting the logging process in a new thread.
This should stop the GUI from freezing.
My current code:
import tkinter as tk
import time
import threading
class GUI:
def __init__(self, tk_object):
self.gui = tk_object
self.gui.title("Logger")
self.gui.resizable(width=True, height=True)
self.testlabel = tk.Label(self.gui, text="Recording in process")
self.testlabel.grid(row = 7, column = 0)
self.testlabel.config(bg='white')
btn1 = tk.Button(master, text="Start Recording", width=16, height=5, command=lambda: self.start_functions())
btn1.grid(row=2,column=0)
btn2 = tk.Button(master, text="Stop Recording", width=16, height=5, command=lambda: self.stop_functions())
btn2.grid(row=3,column=0)
def start_functions(self):
"""Calls the method get_sample in a new thread and changes the bg-color of the label to red"""
Thread_1 = threading.Thread(target=self.get_sample(), name='Thread1')
Thread_1.start()
self.testlabel.config(bg='red')
def stop_functions(self):
"""Stops all active threads and changes bg-color of the label back to white"""
#stop_threads = threading.Event()
#stop_threads.set()
threading.Event().set()
self.testlabel.config(bg='white')
def get_sample(self):
"""get data and append it to a list"""
while not threading.Event().is_set():
time.sleep(1)
res_cel.append(res_cel[-1]+1)
x_value.append(x_value[-1]+1)
print(res_cel)
print(x_value)
master = tk.Tk()
master_object = GUI(master)
master.mainloop()
Currently, the get_sample method contains a placeholder.
I'm trying to stop the Thread1 (where the get_sample method is running in) via the Event() handler.
while not threading.Event().is_set():
This doesn't seem to work in a proper way.
Are there better ways to do this?
Before this approach I tried using a class to handle the threads (This was found on stackoverflow, but I can't seem to find it anymore, sorry):
class Controller(object):
def __init__(self):
self.thread1 = None
self.stop_threads = Event()
def run(self):
.. do somehting
and starting / stopping the threads via:
def starting_thread(self):
self.stop_threads.clear()
self.thread1 = Thread(target = self.run)
self.thread1.start()
def stopping_thread(self):
self.stop_threads.set()
self.thread1 = None
Note: These functions are within the Controller Class.
With this solution I wasn't able to alter the lables background color in the GUI Class, since it doesn't know what object it is referencing to.
Im fairly new to programming python so I'd be glad if someone could explain to me what I'm missing here.
Thanks
xSaturn
You'd better create your own Thread object with a method to stop it. A thread stop running when it goes of its run method. Look at the example bellow
import time
import tkinter as tk
from threading import Thread
class MyThread(Thread):
thread_running = False
def run(self):
k = 0
self.thread_running = True
while self.thread_running:
print(k)
k +=1
time.sleep(1)
print("thread ended")
def stop(self):
self.thread_running = False
class Window(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.thread = None
tk.Button(self, text="launch thread", command=self.launch_thread)\
.grid(row=1, column=0)
tk.Button(self, text="stop thread", command=self.stop_thread)\
.grid(row=2, column=0)
def launch_thread(self):
if self.thread:
print("thread already launched")
else:
print("thread launched")
self.thread = MyThread()
self.thread.start()
def stop_thread(self):
if self.thread:
self.thread.stop()
self.thread = None
else:
print("no thread running")
if __name__ == '__main__':
win = Window()
win.mainloop()
See this topic for more information
Is there any way to kill a Thread?

Python, Tkinter: how can I prevent tkinter gui mainloop crash using threading

Hi I have a small python gui interface with two buttons, start(That starts a counter) and stop (that is suppose to stop the counter), the counter is an infinite loop since I do not want it to end unless the second button is clicked. The problem is the second button cannot be clicked while the function from the first one is still running.
I read that I need to use threading and I have tried but I do not fully understand how I can do this. Please help.
from Tkinter import *
import threading
class Threader(threading.Thread):
def run(self):
for _ in range(10):
print threading.current_thread().getName()
def main(self):
import itertools
for i in itertools.count(1, 1):
print i
def other(self):
print "Other"
m = Threader(name="main")
o = Threader(name="other")
try:
'''From here on we are building the Gui'''
root = Tk()
'''Lets build the GUI'''
'''We need two frames to help sort shit, a left and a right vertical frame'''
leftFrame = Frame(root)
leftFrame.pack(side=LEFT)
rightFrame = Frame(root)
rightFrame.pack(side=RIGHT)
'''Widgets'''
'''Buttons'''
playButton = Button(leftFrame, text="Play", fg="blue", command=m.main)
stopButton = Button(rightFrame, text="Stop", fg="red", command=o.other)
playButton.pack(side=TOP)
stopButton.pack(side=BOTTOM)
root.mainloop()
except Exception, e:
print e
Here's a short example of using threading. I took out your other function and I don't know why your using itertools here. I took that out as well and simply setup using a simple threading example.
A few things:
You setup using threading.Thread as the base class for Threader, but you never actually initialized the base class.
Whenever you use threading you generally want to define a run method and then use start() to start the thread. Calling start() will call run.
You need to use threading to prevent your GUI blocking, because tkinter is just one thread on a giant loop. So, whenever you have some long running process it blocks this thread until the current process is complete. That's why it's put in another thread. Python has something called the GIL, which prevent's true parallelization (I made up that word) since it only one thread can ever be used at a time. Instead, it uses time slicing, the GIL sort of "polls" between them to give the appearance of multiple tasks running concurrently. For true parallel processing you should use multiprocessing.
In the below code I have used self.daemon = True. Setting the thread to be a daemon will kill it when you exit the main program (In this case the Tk GUI)
from tkinter import *
import threading, time
class Threader(threading.Thread):
def __init__(self, *args, **kwargs):
threading.Thread.__init__(self, *args, **kwargs)
self.daemon = True
self.start()
def run(self):
while True:
print("Look a while true loop that doesn't block the GUI!")
print("Current Thread: %s" % self.name)
time.sleep(1)
if __name__ == '__main__':
root = Tk()
leftFrame = Frame(root)
leftFrame.pack(side=LEFT)
rightFrame = Frame(root)
rightFrame.pack(side=RIGHT)
playButton = Button(leftFrame, text="Play", fg="blue",
command= lambda: Threader(name='Play-Thread'))
stopButton = Button(rightFrame, text="Stop", fg="red",
command= lambda: Threader(name='Stop-Thread'))
playButton.pack(side=TOP)
stopButton.pack(side=BOTTOM)
root.mainloop()
For something as simple as a counter, Tkinter's after() method is usually a better choice. You can use an instance variable to set it to on and off.
class TimerTest():
def __init__(self, root):
self.root=root
Button(root, text="Play", fg="blue",
command=self.startit).grid(row=1, column=0)
Button(root, text="Stop", fg="red",
command=self.stopit).grid(row=1, column=1)
self.is_running=True
self.count=IntVar()
Label(root, textvariable=self.count,
bg="lightblue").grid(row=0, column=0, columnspan=2, sticky="ew")
def startit(self):
self.is_running=True
self.increment_counter()
def increment_counter(self):
if self.is_running:
c=self.count.get()
c += 1
self.count.set(c)
root.after(1000, self.increment_counter) ## every second
def stopit(self):
self.is_running = False
root = Tk()
TT=TimerTest(root)
root.mainloop()

How to implement indeterminate Progressbar in Tkinter?

My application has some time-consuming background processes to be run occasionally. I want to show that the application is working using a simple indeterminate ttk.Progressbar.
However, my implementation only shows static progress bar.
Anyway, here is how I implemented it.
class MyApp(ttk.Frame):
def __init__(self, master):
ttk.Frame.__init__(self, master)
fname = 'test.txt'
self.build_ui()
self.process_file(fname)
def process_file(self, fname):
self.show_progress(True)
fdata = self.read_file(fname)
fdata = self.spellcheck(fdata)
self.show_progress(False)
return fdata
def show_progress(self, start)
if start:
self.prog_win = tk.Toplevel()
self.prog_win.title('Working...')
self.prog_win.resizable(0, 0)
self.progress_bar = ttk.Progressbar(self.prog_win,
orient=tk.HORIZONTAL,
mode='indeterminate',
takefocus=True)
self.progress_bar.grid()
self.progress_bar.start()
else:
self.progress_bar.stop()
self.prog_win.destroy()
root = tk.Tk()
root.update()
gui = MyApp(root)
gui.mainloop()
The code above does not work as intended. The Progressbar appears static, it does not move and just hangs there forever. I tried to use threading but if I start show_progress in a separate Thread it always gets executed after the processing has been done.
What am I doing wrong?
The problem is that self.read_file() and self.spellcheck() are blocking which prevents Tkinter from updating your progress bar in its main loop. A simple way to solve this is to do your processing in a separate thread, and periodically check to see if the thread finished its work.
import threading
class MyApp(ttk.Frame):
def __init__(self, master):
ttk.Frame.__init__(self, master)
fname = 'test.txt'
self.build_ui()
self.process_file(fname)
def process_file(self, fname):
self.show_progress(True)
# Start thread to process file.
self.thread = threading.Thread(target=self.process_file_worker, args=(fname,))
self.thread.daemon = True # Allow the program to terminate without waiting for the thread to finish.
self.thread.start()
# Start checking the thread.
self.process_file_check()
def process_file_check(self):
if self.thread.is_alive():
# Thread is still running, check thread again in 10 milliseconds.
self.after(10, self.process_file_check)
else:
# Thread finished, handle processed results.
# Do something with `self.fdata`.
self.show_progress(False)
def process_file_worker(self, fname):
# This is run inside the thread.
fdata = self.read_file(fname)
fdata = self.spellcheck(fdata)
self.fdata = fdata
def show_progress(self, start):
if start:
self.prog_win = tk.Toplevel()
self.prog_win.title('Working...')
self.prog_win.resizable(0, 0)
self.progress_bar = ttk.Progressbar(self.prog_win,
orient=tk.HORIZONTAL,
mode='indeterminate',
takefocus=True)
self.progress_bar.grid()
self.progress_bar.start()
else:
self.progress_bar.stop()
self.prog_win.destroy()
root = tk.Tk()
root.update()
gui = MyApp(root)
gui.mainloop()

Categories