Tkinter: stopping a background thread on demand
vriolk at gmail.com
Thu Feb 19 17:20:14 EST 2009
I am writing a Tk GUI for a console script. I start a background
thread and pass it a callback function which writes debug info to a
Text widget in the main Tk loop.
I tried to find what is the established way to cleanly abort a child
thread when the user clicks the Stop button, but apparently Python
does not allow the parent process to explicitly stop a child thread.
Also I could not figure out how a parent can send a signal for a child
to stop and let it cleanup and exit cleanly.
I finally figured out a solution which was to set a 'TERMINATE' flag
on the `func_dict` property of the callback function, and then check
for the flag in the child module, but I still have doubts that this is
the proper way to do this sort of thing.
This works fine but it looks more like a dirty hack to me so my
question is: what is the proper way to send a signal to a child thread
to gracefully terminate, assuming the user decided to cancel an
from Tkinter import *
root = Tk()
def __init__(self, source, target, callbackFunc):
self.infile = srcName
self.outfile = destName
self.callbackFunc = callbackFunc
if (job and job.isAlive()):
callbackFunc.func_dict['TERMINATE'] = True
job = None
job = ConverterThread(src='file1', dest='file2, updateLogCallback)
stopBtn = Button(root, text="Stop", command=stopConversion)
log = Text(root)
And the callbackFunc in myConverter.py looks like this:
if callback.func_dict['TERMINATE'] == True:
More information about the Python-list