2016-06-14 214 views
1

我目前正在創建一個將Tkinter輸入框插入Google Calendar的小程序(在所有不同類型的課程檢查之後)。這部分不是問題。 由於我在Tkinter窗口打開期間不想「保持」的同時運行終端。Python - 爲什麼我的Tkinter窗口沒有關閉self.destroy?

當我關閉使用

def quit(self): 
    self.thread.stop() 
    self.destroy() 

窗口的所有部分消失的窗口,但窗口停留在屏幕上。

class window(Frame): 

def __init__(self, parent, thread): 
    Frame.__init__(self, parent) 

    self.parent = parent 
    self.w = 600 
    self.h = 400 

    self.initUI() 
    self.center() 
    self.thread = thread 

我用這個功能可按創建類:

def main(thread): 

root = Tk() 
root.resizable(width=False, height=False) 
app = window(root, thread) 
root.mainloop() 

的MyThread的類。

class myThread(threading.Thread): 
def __init__(self,threadType): 
    threading.Thread.__init__(self) 
    self.threadType = threadType 
    self.event = threading.Event() 
def stop(self): 
    self.event.set() 
def run(self): 
    if self.threadType == "new_app": 
     newappoint.main(self) 
    elif self.threadType == "main": 
     main() 

有人可以告訴我,如果我錯過了一些東西,會使窗戶關閉正常。 線程關閉後調用self.quit()

+0

tkinter和線程不好混,看例如http://stackoverflow.com/questions/10556479/running-a-tkinter-form-in-a-separate-thread –

+0

@shivsn這工作,謝謝你: ) –

+0

@JJHakala所以基本上,最好只使用我原來的主函數,然後讓'if __name__ == __main __:'循環尋找一個變量來更改並啓動Tkinter? –

回答

0

而不是self.destroy()這樣做self.parent.destroy()

至於我的理解我們傳遞tk對象的類window並在class.So作爲parent而不是使用root創建我們使用parent部件。

+0

你能解釋爲什麼它有幫助嗎? – IanS

+0

謝謝。你應該將這個解釋添加到答案中。 – IanS