我在 tkinter Toplevel 小部件方面遇到了一些问题。每当我尝试销毁顶层窗口时,它都不会关闭,而是变为非事件状态。每当我单击本应被销毁的窗口时,我都会收到内存地址错误。有人知道我做错了什么吗?
示例代码:
from tkinter import *
import _thread as thread
import time
root = Tk()
root.title('root')
login = Toplevel(root)
login.title('login')
thread.start_new_thread(root.mainloop,())
time.sleep(3)
login.destroy()
最佳答案
Tkinter 不是线程安全的。所有 Tkinter 调用都应源自单个线程。使用 root.after
安排函数调用在延迟后发生。
from tkinter import *
root = Tk()
root.title('root')
login = Toplevel(root)
login.title('login')
root.after(3000, login.destroy)
root.mainloop()
根据 TkinterSummary :
All Tkinter access must be from the main thread (or, more precisely, the thread that called mainloop). Violating this is likely to cause nasty and mysterious symptoms such as freezes or core dumps.
关于python tkinter 销毁顶层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23110048/