在我的程序中,我想在按鈕上使用forget()
。現在,如果我嘗試,程序崩潰。我知道它與線程有關,但我還是找不到靈魂。提前致謝。這裏是我的examplecode:Python Tkinter程序崩潰與忘記()
import Tkinter as tk
import thread
window = tk.Tk()
def ok():
pass
def voice():
button1.forget()
print("If you see this, it works!")
thread.start_new_thread(voice,())
button1=tk.Button(command=ok, text="PRESS")
button1.pack()
window.mainloop()
在創建它的線程之外的線程中訪問小部件往往會使Tkinter翻轉並崩潰。爲什麼在這裏使用線程?直接使用Button的命令'voice'即可。 – Kevin 2014-12-05 17:22:32
@kevin嗯,這只是一個示例代碼。在我真正的程序中,我使用pyttsx,爲此我需要線程。 – Duardo 2014-12-05 17:31:03
也許你可以重構你的代碼,以便所有的Tkinter特定的動作發生在主線程上。例如,語音將全局'should_forget'設置爲True,並且根具有'after_idle'回調方法,該方法定期檢查'should_forget'是否變爲True,並且適當地調用'forget'。 – Kevin 2014-12-05 17:33:28