当加载图标且 tk.mainloop 位于线程中时,Tkinter 会锁定 Python
这是测试用例...
import Tkinter as tk
import thread
from time import sleep
if __name__ == '__main__':
t = tk.Tk()
thread.start_new_thread(t.mainloop, ())
# t.iconbitmap('icon.ico')
b = tk.Button(text='test', command=exit)
b.grid(row=0)
while 1:
sleep(1)
该代码有效。 取消注释 t.iconbitmap 行并将其锁定。 以您喜欢的方式重新排列; 它会锁定。
当存在图标时,如何防止 tk.mainloop 锁定 GIL ?
目标是win32和Python 2.6.2。
Here's the test case...
import Tkinter as tk
import thread
from time import sleep
if __name__ == '__main__':
t = tk.Tk()
thread.start_new_thread(t.mainloop, ())
# t.iconbitmap('icon.ico')
b = tk.Button(text='test', command=exit)
b.grid(row=0)
while 1:
sleep(1)
This code works. Uncomment the t.iconbitmap line and it locks. Re-arrange it any way you like; it will lock.
How do I prevent tk.mainloop locking the GIL when there is an icon present?
The target is win32 and Python 2.6.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信你不应该在不同的线程上执行主循环。 AFAIK,主循环应该在创建小部件的同一线程上执行。
我熟悉的 GUI 工具包(Tkinter、.NET Windows Forms)是这样的:您只能从一个线程操作 GUI。
在 Linux 上,您的代码引发异常:
以下之一将起作用(无额外线程):
使用额外线程:
如果您需要从 tkinter 线程外部与 tkinter 进行通信,我建议您设置一个计时器来检查队列为了工作。
这是一个例子:
I believe you should not execute the main loop on a different thread. AFAIK, the main loop should be executed on the same thread that created the widget.
The GUI toolkits that I am familiar with (Tkinter, .NET Windows Forms) are that way: You can manipulate the GUI from one thread only.
On Linux, your code raises an exception:
One of the following will work (no extra threads):
With extra thread:
If you need to do communicate with tkinter from outside the tkinter thread, I suggest you set up a timer that checks a queue for work.
Here is an example: