Text widget updates only after calling method exits (threading issue?)

mariox19 mariodiana at gmail.com
Wed Dec 12 19:08:28 EST 2007


*** SOLVED ***

Thanks, Eric. I've had luck with code along these lines:

    # 1. Assume Text widget as instance variable: textView
    # 2. Assume button with method, 'start', bound to it

    def start(self, event=None):
        """
        Starts the demo.
        """
        # Print A-Z to widget, pausing 1/10 second between each letter
        for aNumber in range(65, 91):
            self.textView.insert(END, chr(aNumber))
            self.textView.update_idletasks()
            time.sleep(0.1)

The code does just what I want it to.

Mario

On Dec 12, 3:43 am, "Eric Brunel" <see.signat... at no.spam> wrote:
> On Wed, 12 Dec 2007 02:58:37 +0100, mariox19 <mariodi... at gmail.com> wrote:
> > Are Tkinter widgets running on their own thread?
>
> No. And usually, GUI toolkits and threads don't mix well...
>
>
>
> > If I try to make a simple application that will print the letters A to
> > Z to a Tkinter Text widget, and I space the printing of each letter by
> > 1 second, it seems no text will appear in the Text widget until the
> > method exits.
>
> > Take a look at this snippet:
>
> >     # Assume I have created no threads other than the one that comes
> > with Main
>
> >     def printToTkinterTextWidget(text):
> >         """
> >         Prints A-Z to the Text widget, 1 letter per second.
> >         """
> >         # Problem: no text appears in the widget until 26 seconds has
> > elapsed
> >         for aNumber in range(65, 91):
> >             self.textWidget.insert(END, text)
> >             time.sleep(1)
>
> time.sleep will not give back the control to the Tkinter mainloop, so your
> text widget won't be refreshed on screen. Try:
> self.textWidget.update_idletasks()
> before the sleep.
>
> HTH
> --
> python -c "print ''.join([chr(154 - ord(c)) for c in
> 'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"




More information about the Python-list mailing list