[Tkinter-discuss] TclStackFree Error
Trevor J. Christensen
trevor at jcmanagement.net
Fri Feb 11 16:20:27 CET 2011
> The problem is that you must _n e v e r_ access the tk event loop
> from more than one thread. The best practice is to always run the
> mainloop () within the main program thread and, if child threads are
> really necessary, to use e.g. threading.Condition or Queue.Queue
> objects to handle the communication between Tkinter and the child
> thread.
> Michael
Thank you for your help. Yes, you are right. Child threads must not
control Tk and must communicate via share a thread communication
mechanism. The following is my solution which works very well:
import threading, time, socket
from Tkinter import *
EVENT = '<<EVENT>>'
PORT = 12339
ADDR = ('', PORT)
BUFSIZ = 1024
class Monitor(object):
def __init__(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.settimeout(0.0) # non-blocking
self.s.bind(ADDR)
self.run()
def run(self):
while True:
try:
data, addr = self.s.recvfrom(BUFSIZ)
except socket.error, msg:
break
# I modified Tkinter.py slightly to support the 'data' key
text.event_generate(EVENT, data=data)
root.after(500, self.run) # Reschedule
def On_EVENT(*args):
event = args[0]
text.insert(END, event.data)
try: # Handle Tk vertical scroll bar bug
v = vbar.get()[1]
except ValueError:
v = 0.0
if not v < 0.95: # Support user review of history
text.see(tk.END)
text.see(END)
root = Tk()
#---------------------------------------
text = Text(root)
vbar = Scrollbar(root, orient=VERTICAL)
vbar['command'] = text.yview
text['yscrollcommand'] = vbar.set
#---------------------------------------
text.grid(column=0, row=0, sticky=NSEW)
vbar.grid(column=1, row=0, sticky=NS)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
#---------------------------------------
text.bind(EVENT, On_EVENT)
#---------------------------------------
worker = Monitor()
#---------------------------------------
root.mainloop()
Trevor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20110211/deb453d8/attachment.html>
More information about the Tkinter-discuss
mailing list