Python 2.7
I have written a run method for my Tkinter GUI rather than using the standard mainloop, and it always exits on an error when I close the window, even after implementing a WM_DELETE_WINDOW protocol as advised elsewhere on SO. I tried invoking exit in the protocol callback and returning from the loop, but Python always goes through the loop one last time. Why is this?
class FrameApp(object):
def __init__(self):
...
self.rootWin.protocol("WM_DELETE_WINDOW", self.callback_destroy)
self.winRunning = False
def callback_destroy(self):
self.winRunning = False
self.rootWin.destroy() # go away, window
exit() # GET OUT
Here is the run loop:
def run(self):
last = -infty
self.winRunning = True
...
while self.winRunning:
# 4.a. Calc geometry
self.calcFunc( self.get_sliders_as_list() )
# 4.b. Send new coords to segments
self.simFrame.transform_contents()
# 4.d. Wait remainder of 40ms
elapsed = time.time() * 1000 - last
if elapsed < 40:
time.sleep( (40 - elapsed) / 1000.0 )
# 4.e. Mark beginning of next loop
last = time.time() * 1000
# 4.f. Update window
if not self.winRunning: # This does not solve the problem
return # still tries to call 'update',
# and never exits cleanly
self.canvas.update()
# don't know how to prevent these from being called
# again after the window is destroyed
self.rootWin.update_idletasks()
Result:
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 972, in update_idletasks self.tk.call('update', 'idletasks') _tkinter.TclError: can't invoke "update" command: application has been destroyed
mainloop? - David Gomesself.calcFunc, in the file that instantiates theFrameApp) and they aren't yet heavy enough to have their own thread. ~ I'm trying to make this modular so that aFrameAppcan run anycalcFuncthat gets assigned to it. - SquareCrowwhile True : if my_end_signal occured : break. Detectend pointbefore destroy everthing ! - dsgdfgif not self.winRunningclause within the loop. When the running flag property is set to false by the window destroy protocol, it is supposed to break the loop. I have tried bothreturnandbreakas a response to an exit flag, but I get the same error in both cases. - SquareCrowpython FILENAME.py. My usual IDE, Spyder, will sometimes cause many windows to be created if I launch a Tkinter script from it. - SquareCrow