473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Dec 12 '07 #1
3 1640
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
Dec 12 '07 #2
On Wed, 12 Dec 2007 02:58:37 +0100, mariox19 <ma********@gmail.comwrote:
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+-'])"
Dec 12 '07 #3
*** 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...@no.spamwrote:
On Wed, 12 Dec 2007 02:58:37 +0100, mariox19 <mariodi...@gmail.comwrote:
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+-'])"
Dec 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: bigbinc | last post by:
I have used the 'entry' tk widget to get text values, I am now using 'Text' but I cant seem to use 'get' method. The TK docs say use get(index1, index2), I tried numbers and get an error ...
2
by: Tonino | last post by:
Hi, I have a small Tkinter app that gets data from a socket connection to a "server". The app has a Text() widget to display the info that it gets from the socket connection. I have the...
14
by: Christopher Subich | last post by:
As a hobby project, I'm writing a MUD client -- this scratches an itch, and is also a good excuse to become familiar with the Python language. I have a conceptual handle on most of the...
0
by: William Gill | last post by:
I have a Tkinter (frame) widget that contains several other frame widgets, each containing entry widgets. In the parent frame I have a 'save' button that is initially disabled. As it is now, each...
7
by: Ken Fine | last post by:
My well-heeled clients would like a tool that I could build into a web application that would suck down the entire contents a remote webpage and store it on a local filesystem. I know how to...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
5
by: deacon.sweeney | last post by:
Hi, I've been searching for a .resize()-like function to overload much like can be done for the delete window protocol as follows: toplevel.protocol("WM_DELETE_WINDOW", callback) I realize...
0
by: John McMoangle | last post by:
Hey guys. I am having trouble understanding the get() method from The arguments to the get method of the Text widget are strings in the form of "line.position". So, text.get('1.5', '2.7')...
4
by: Mudcat | last post by:
I've tried quite a few things to get this correct but have hit a couple of sticking points that I can't figure out. I need to ge the Text box to function like the 'wraplength' option in a Label. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.