[Tkinter] text widget update problem

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Mon Dec 2 15:55:11 EST 2002


On Fri, 2002-11-29 at 13:38, Thomas Rademacher wrote:
> Hallo Martin,
> 
> > On Thu, 2002-11-28 at 10:01, Thomas Rademacher wrote:

> >
> > Are you calling update() from the main thread (you don't mention threads
> > but I am guessing you are using them)  Could you post a cut down version
> > that demonstrates the problem?  I have had problems with calling
> > update() from another thread.....
> >
> 
> Yes!!! I use threads:
> 

Not really I tried the following (and I seem to remember it worked) but
stopped using threads for the application I was writnig at the time (it
just didn't need them!.


Basically the worker thread(s) use the print statement to 'print' their
logging information and I redirect sys.stdout & sys.stderr to the text
box, for this to work the text box has to have a write method.  

So I started by suvclassing a (Pmw) ScroledTextBox and gave it a write()
method like so:-

class MyScrolledText(Pmw.ScrolledText):
    def __init__(self, parent = None, **kw):
        # Define the megawidget options.
        INITOPT = Pmw.INITOPT
        optiondefs = (
            ('borderframe',    0,            INITOPT),
            ('hscrollmode',    'dynamic',    self._hscrollMode),
            ('labelmargin',    0,            INITOPT),
            ('labelpos',       None,         INITOPT),
            ('scrollmargin',   2,            INITOPT),
            ('usehullsize',    0,            INITOPT),
            ('vscrollmode',    'dynamic',    self._vscrollMode),
        )
        self.defineoptions(kw, optiondefs)
        Pmw.ScrolledText.__init__(self, parent)
        self.initialiseoptions(MyScrolledText)

    def write(self, stuff):
        self.insert('end',stuff)
        self.yview_pickplace('end')
        self.update()





The when I created and instance of this widget in my main application I
redirect standard error and standard out to it, kicked off a few threads
sit back and watch.....


root=Tk()
mst=MyScrolledText(root)
mst.pack(fill='both', expand='yes')
sys.stderr=mst
sys.stdout=mst



def DoThing(a):        
    thing=open('Pmw/Pmw.py')
    while 1:
        line=thing.readline()
        if not line: break
        print a, ":", line
    
for n in range(10):
    t=threading.Thread(target=DoThing, args=(n, ))
    t.start()


root.mainloop()



HTH
Martin.












More information about the Python-list mailing list