FAQ
Are Tkinter widgets running on their own thread?

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)

If I am supposed to send messages to Tkinter objects only from the
main thread, how can I get the letters to appear 1 per second?

Thanks,

Mario

Search Discussions

  • Marc 'BlackJack' Rintsch at Dec 12, 2007 at 7:46 am

    On Tue, 11 Dec 2007 17:58:37 -0800, mariox19 wrote:

    If I am supposed to send messages to Tkinter objects only from the
    main thread, how can I get the letters to appear 1 per second?
    Take a look at the `after()` method on widgets.

    Ciao,
    Marc 'BlackJack' Rintsch
  • Eric Brunel at Dec 12, 2007 at 8:43 am

    On Wed, 12 Dec 2007 02:58:37 +0100, mariox19 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+-'])"
  • Mariox19 at Dec 13, 2007 at 12:08 am
    *** 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" wrote:
    On Wed, 12 Dec 2007 02:58:37 +0100, mariox19 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+-'])"

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 12, '07 at 1:58a
activeDec 13, '07 at 12:08a
posts4
users3
websitepython.org

People

Translate

site design / logo © 2023 Grokbase