的Python 2.7Tkinter的循環永遠不會乾淨地退出
我已經寫了run
方法我Tkinter的GUI,而不是使用標準mainloop
,它總是在錯誤退出,當我關閉窗口,甚至實施WM_DELETE_WINDOW
協議後,正如其他地方建議的那樣。我嘗試在回調中調用exit
,並且從循環中調用return
,但Python總是最後一次通過循環。爲什麼是這樣?
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
這裏是運行循環:
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()
結果:
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
爲什麼你不使用tkinter的'mainloop'? –
我沒有使用它,因爲我在循環之外做了一些計算('self.calcFunc',在實例化'FrameApp'的文件中),並且它們還不夠重,無法擁有自己的線程。 〜我試圖使這個模塊化,以便'FrameApp'可以運行任何分配給它的'calcFunc'。 – SquareCrow
你的循環需要打破內部(所以如果完成),如何管理所有的標誌?意思是「while True:如果my_end_signal發生:break'。在破壞之前檢測'終點'! – dsgdfg