Tkinter.Tk() 和线程 [英] Tkinter.Tk() and threading

查看:36
本文介绍了Tkinter.Tk() 和线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tkinter 和线程有一个有趣的问题:我有一个基于 Tkinter 的 GUI 和一些与 mainloop 一起执行的代码.如果我只做一次,它就像魅力一样.但是如果我做两次 Tkinter.Tk() 会阻塞两个线程:GUI 和 MainThread.

There is an interesting issue with Tkinter and threading: I have a Tkinter based GUI and some code executed alongside mainloop. It works like charm if I only do it once. But if i do it twice Tkinter.Tk() blocks BOTH threads: GUI and MainThread.

这是代码(灵感来自 另一个 Tkinter 与线程主题):

here is the code (inspired by another Tkinter vs threading topic):

import Tkinter 
import threading
import logging

logging.basicConfig(level=logging.DEBUG, format="%(asctime)s  %(levelname)s: %(message)s")

def gui():
    logging.info("Starting GUI")
    root = Tkinter.Tk() 
    logging.info("Setup GUI")
    root.after(500, root.destroy)
    root.mainloop()
    logging.info("Mainloop is over")

def start_gui_thread():
    gui_thread =  threading.Thread(None, gui, None)
    gui_thread.setDaemon(True)
    gui_thread.start()    
    return gui_thread

def test():
    gui_thread = start_gui_thread()    
    logging.info("Do something alongside gui")

    logging.info("Wait for gui to terminate")
    gui_thread.join()
    logging.info("thread terminated")

logging.info("--------- 1 ---------")
test()
logging.info("--------- 2 ---------")
test()

结果:winxp + python 2.7.1(默认安装)

result: winxp + python 2.7.1 (default installation)

INFO: --------- 1 ---------
INFO: Do something alongside gui
INFO: Starting GUI
INFO: Wait for gui to terminate
INFO: Setup GUI
INFO: Mainloop is over
INFO: thread terminated
INFO: --------- 2 ---------
INFO: Starting GUI
INFO: Do something alongside gui

更新结果与 OS X 10.6.7 + python 2.7.1

UPD result with OS X 10.6.7 + python 2.7.1

MacBook-Pro:~ igann$ python test.py 
2012-07-05 21:01:52,939  INFO: --------- 1 ---------
2012-07-05 21:01:52,940  INFO: Starting GUI
2012-07-05 21:01:54,835  INFO: Do something alongside gui
2012-07-05 21:01:54,835  INFO: Wait for gui to terminate
2012-07-05 21:01:54,835  INFO: Setup GUI
2012-07-05 21:01:55,337  INFO: Mainloop is over
2012-07-05 21:01:55,339  INFO: thread terminated
2012-07-05 21:01:55,339  INFO: --------- 2 ---------
2012-07-05 21:01:55,339  INFO: Starting GUI
2012-07-05 21:01:55,366  INFO: Do something alongside gui
2012-07-05 21:01:55,366  INFO: Wait for gui to terminate
2012-07-05 21:01:55,367  INFO: Setup GUI
Tcl_WaitForEvent: CFRunLoop finished
Abort trap
MacBook-Pro:~ igann$ 

推荐答案

它对我来说很好用(OS X 10.7,Python 2.7.1)所以可能与您的安装有关:

It works fine for me (OS X 10.7, Python 2.7.1) so maybe it's related to your installation:

pu@pumbair:~/Downloads$ python test.py 
2012-07-05 19:22:19,673  INFO: --------- 1 ---------
2012-07-05 19:22:19,673  INFO: Starting GUI
2012-07-05 19:22:19,674  INFO: Do something alongside guy
2012-07-05 19:22:19,918  INFO: Wait for gui to terminate
2012-07-05 19:22:19,918  INFO: Setup GUI
2012-07-05 19:22:20,422  INFO: Mainloop is over
2012-07-05 19:22:20,424  INFO: thread terminated
2012-07-05 19:22:20,424  INFO: --------- 2 ---------
2012-07-05 19:22:20,425  INFO: Starting GUI
2012-07-05 19:22:20,459  INFO: Do something alongside gui
2012-07-05 19:22:20,459  INFO: Setup GUI
2012-07-05 19:22:20,459  INFO: Wait for gui to terminate
2012-07-05 19:22:20,962  INFO: Mainloop is over
2012-07-05 19:22:20,965  INFO: thread terminated

pu@pumbair:~/Downloads$ python
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

当然,操作并不总是以相同的顺序执行(与上面比较):

but operations do not always execute in the same order, of course (compare this with above):

pu@pumbair:~/Downloads$ python test.py 
2012-07-05 19:27:17,827  INFO: --------- 1 ---------
2012-07-05 19:27:17,827  INFO: Starting GUI
2012-07-05 19:27:17,827  INFO: Do something alongside gui
2012-07-05 19:27:17,937  INFO: Setup GUI
2012-07-05 19:27:17,938  INFO: Wait for gui to terminate
2012-07-05 19:27:18,440  INFO: Mainloop is over
2012-07-05 19:27:18,442  INFO: thread terminated
2012-07-05 19:27:18,443  INFO: --------- 2 ---------
2012-07-05 19:27:18,443  INFO: Starting GUI
2012-07-05 19:27:18,478  INFO: Do something alongside gui
2012-07-05 19:27:18,478  INFO: Setup GUI
2012-07-05 19:27:18,479  INFO: Wait for gui to terminate
2012-07-05 19:27:18,981  INFO: Mainloop is over
2012-07-05 19:27:18,982  INFO: thread terminated

在 10.6.8 和 Python 2.6.1 上它也可以工作,但有奇怪的消息:

On 10.6.8 with Python 2.6.1 it also works, but with strange messages:

symacbook:Downloads putest$ python test.py 
2012-07-05 21:33:49,844  INFO: --------- 1 ---------
2012-07-05 21:33:49,844  INFO: Starting GUI
2012-07-05 21:33:49,844  INFO: Do something alongside gui
2012-07-05 21:33:52,356  INFO: Wait for gui to terminate
2012-07-05 21:33:52,356  INFO: Setup GUI
2012-07-05 21:33:52,858  INFO: Mainloop is over
2012-07-05 21:33:52,859  INFO: thread terminated
2012-07-05 21:33:52,860  INFO: --------- 2 ---------
2012-07-05 21:33:52,860  INFO: Starting GUI
2012-07-05 21:33:52,892  INFO: Setup GUI
2012-07-05 21:33:52,892  INFO: Do something alongside gui
2012-07-05 21:33:52,894  INFO: Wait for gui to terminate
2012-07-05 21:33:52.894 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002d9d90 of class NSCFArray autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.896 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002d51e0 of class NSFont autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.897 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x7fff70737588 of class NSCFString autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.897 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101233b40 of class _NSThemeCloseWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.898 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e0ef0 of class NSCFArray autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.898 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002d51e0 of class NSFont autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.899 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x7fff70737588 of class NSCFString autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.899 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1170 of class _NSThemeWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.899 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x10120e9c0 of class NSCFArray autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.900 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002d51e0 of class NSFont autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.900 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x7fff70737588 of class NSCFString autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.901 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1710 of class _NSThemeWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.901 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e18e0 of class NSCFArray autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.902 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1012343e0 of class NSTrackingArea autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.902 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101234010 of class NSTrackingArea autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.903 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1910 of class NSView autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.904 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002d3550 of class NSCFArray autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.905 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1910 of class NSView autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.905 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101234840 of class NSCFArray autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.906 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1d90 of class NSCFArray autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.906 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101234870 of class __NSFastEnumerationEnumerator autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.907 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101233b40 of class _NSThemeCloseWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.907 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101233b40 of class _NSThemeCloseWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.908 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1710 of class _NSThemeWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.908 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1710 of class _NSThemeWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.908 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1170 of class _NSThemeWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.909 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1170 of class _NSThemeWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.909 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e22e0 of class NSTrackingArea autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.910 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e24d0 of class NSConcreteAttributedString autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.911 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x7fff70737588 of class NSCFString autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.911 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002d51e0 of class NSFont autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.911 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002d91f0 of class NSFont autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.912 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x7fff70737588 of class NSCFString autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.912 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e28b0 of class NSTrackingArea autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.913 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e3ab0 of class NSConcreteMapTable autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.914 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e3e30 of class NSCFArray autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.915 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1dc0 of class NSObject autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.915 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e4120 of class NSCFArray autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.916 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101234a60 of class NSMenuItem autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.916 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002ac2e0 of class NSCFNumber autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.916 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e4180 of class NSCFDictionary autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.917 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x10120e770 of class NSCFNumber autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.917 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1012031d0 of class NSCFDictionary autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.918 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101233b40 of class _NSThemeCloseWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.919 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101233b40 of class _NSThemeCloseWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.919 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1710 of class _NSThemeWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.919 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1710 of class _NSThemeWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.920 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1170 of class _NSThemeWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:52.920 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e1170 of class _NSThemeWidget autoreleased with no pool in place - just leaking
2012-07-05 21:33:53.393 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101236910 of class NSCFArray autoreleased with no pool in place - just leaking
2012-07-05 21:33:53.393 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x10120e770 of class NSCFNumber autoreleased with no pool in place - just leaking
2012-07-05 21:33:53.394 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101236ce0 of class NSCFDictionary autoreleased with no pool in place - just leaking
2012-07-05 21:33:53.394 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002e24b0 of class NSCFString autoreleased with no pool in place - just leaking
2012-07-05 21:33:53.395 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x1002ac2e0 of class NSCFNumber autoreleased with no pool in place - just leaking
2012-07-05 21:33:53.395 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101236d20 of class NSCFDictionary autoreleased with no pool in place - just leaking
2012-07-05 21:33:53.396 Python[1281:3407] *** __NSAutoreleaseNoPool(): Object 0x101236e70 of class NSCFArray autoreleased with no pool in place - just leaking
2012-07-05 21:33:53,398  INFO: Mainloop is over
2012-07-05 21:33:53,400  INFO: thread terminated

这篇关于Tkinter.Tk() 和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆