> Hi Trevor,
> 
> Thus spoketh "Trevor J. Christensen" <tre...@jcmanagement.net> 
> unto us on Wed, 9 Feb 2011 09:54:13 -0700:
> 
> > Under Windows XP, the following simple program crashes after 20 
> > seconds or so with the following error:
> >  
> > TclStackFree: incorrect freePtr. Call out of sequence?
> >  
> > It behaves the same under python 2.6 and python 2.7.
> >  
> > I suspect it has something to do with Tk/Tcl's 
> multithreading model.  
> > I did discover that if you comment out the vertical scrollbar code, 
> > this program will run fine.  I could use some help here in figuring 
> > out what the problem is.
> 
> 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.
> 
> Regards
> 
> Michael
> 
Michael, thank you for your help.  The following is my final 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(200, 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()

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to