Text widget updates only after calling method exits (threadingissue?)

M

mariox19

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
 
M

Marc 'BlackJack' Rintsch

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
 
D

Dennis Lee Bieber

Are Tkinter widgets running on their own thread?
No... But screen updates tend to take place during the idle periods
between event handlers -- IE, when event handlers return to the
"mainloop" processing.
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)
Uhm... that snippet does NOTHING with aNumber (which I'd probably
have coded as for aNumber in range(ord("A"), ord("A") + 26:
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?
Uhm... Setup a repeating ".after()" action on the main frame which
outputs the next character, resets the ".after()" action, and exits?

Or... maybe invoke a ".update()" action to render the screen -- but
this version will be oblivious to any mouse/key activity until the
function doing all the .sleep() calls exits!
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
E

Eric Brunel

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
 
M

mariox19

*** 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top