Tkinter&Threads异常:堆栈空间不足(无限循环?)

2024-10-01 15:36:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我们使用Tkinter GUI实现了一个分布式聊天。当我将系统更新到Fedora18时,Im在调用Tkinter事件时出现异常,与所描述的here几乎相同:

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 551, in bootstrap_inner self.run()
File "/usr/lib64/python2.7/threading.py", line 504, in run self.target(*self.__args, **self.__kwargs)
File "/hachat/peer.py", line 156, in startRecvLoop
self.processMessage(msg, addr)
File "/hachat/peer.py", line 222, in processMessage
self.gui.receive(msg) 
File "/hachat/gui.py", line 74, in receive
self.textfenster.insert(END,msg.name+": "+msg.text+'\n')
File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 2986, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
TclError: out of stack space (infinite loop?)

下面是从gui类中截取的一个:

^{pr2}$

这个异常只出现在我的系统上,原因似乎是tk没有在编译时支持线程。我必须去掉这个例外——因为程序是分布式的,它需要在不同的系统上运行。因此,我在询问如何消除这个异常,并提示获取tk以支持线程。 Im使用的Python版本是2.7.3,Tcl/Tk版本是8.5。import Tkinter; Tkinter.Tk().tk.eval("puts $tcl_platform(threaded)")也返回了一个异常。在


Tags: inpyselftkinterusr系统line分布式
2条回答

这一个有效:

from Tkinter import *
from ScrolledText import ScrolledText
from threading import Thread

scrolled = None

def start():
    global scrolled
    root = Tk()
    scrolled = ScrolledText(root)
    scrolled.pack(fill=BOTH, expand=YES)
    return root

Thread(target=start().mainloop).start()

print scrolled.get(0.0, END)

我解决了排队与Tk通信的问题。请参见Mutli-threading python with Tkinter获取示例!在

相关问题 更多 >

    热门问题