Tkinter & Threads - Exception: from stack space (infinite loop?)
We have implemented a distributed chat that uses the Tkinter GUI. When I upgraded my system to Fedora18, I get exceptions when the Tkinter event is called, much the same as described 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?)
Here is a cut from the gui class:
import Tkinter
import ScrolledText
import tkMessageBox
import tkSimpleDialog
import threading
class gui(object):
def __init__(self, parent):
self.root = Tkinter.Tk()
self.textfenster = ScrolledText.ScrolledText(self.fpopup,width=90,height=24,background='white')
self.textfenster.pack(fill=Tkinter.BOTH, expand=Tkinter.YES)
def run(self):
self.guiRunThread = threading.Thread(target=self.root.mainloop())
self.guiRunThread.daemon = True
self.guiRunThread.start()
def receive(self,msg):
self.textfenster.insert(Tkinter.END,msg.name+": "+msg.text+'\n')
self.textfenster.see(Tkinter.END)
The exception only appears on my system, the reason is that tk was not compiled with thread support. I have to get rid of this Exeption - as the program gets distributed it has to work on different systems. So I'm asking how to get rid of this exception as well as a hint of getting tk to support threads. I am using Python 2.7.3, Tcl / Tk version 8.5. import Tkinter; Tkinter.Tk().tk.eval("puts $tcl_platform(threaded)")
also returns an exception.
source to share
I solved the problem with Tk communication queues. See Mutli-threading python with Tkinter for an example!
source to share
It works:
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)
source to share