[Tutor] Simple Tkinter question (fwd)
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Fri Oct 7 07:15:05 CEST 2005
---------- Forwarded message ----------
Date: Thu, 6 Oct 2005 22:07:50 -0700 (PDT)
From: Mike Cheponis <mac at Wireless.Com>
To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] Simple Tkinter question
On Thu, 6 Oct 2005, Danny Yoo wrote:
>> from Tkinter import *
>> from time import *
>>
>> def my_update():
>> for i in range(3):
>> tv.set("Now it's %d"%i)
>> sleep(1)
>>
>> root=Tk()
>> tv=StringVar()
>> Entry(textvariable=tv).pack()
>> tv.set("Initial Value of StringVar")
>> Button(text="Update", command=my_update).pack()
>> root.mainloop()
>
>
> Hi Mike,
>
> The problem is that the GUI needs control back to be able to update the
> GUI display.
Thanks, but what I don't understand is:
Why doesn't the call tv.set("string") update what's on the screen. Clearly,
Tkinter has control at that point, and there's no reason to delay updating the screen,
is there?
What I'm really trying to do is:
I have a bunch of lines selected on the screen. I'm using Pmw. This returned
tuple has a number of items I need to play. While the sound is playing, I want
to display the name of the sound on the GUI. (The playing is done by a routine
that writes to the serial port - that is, I call a function with the filename of
the sound, and it plays it).
I'll see if I can get something from the URL you mention, perhaps calling after_idle().
Thanks again!
-Mike
> Tkinter has a particular way of doing sleep()-like behavior that interacts
> better with the GUI. We can use the "after()" method to tell the GUI to
> do some action after some period of time.
>
> Here's a quick-and-dirty example:
>
> ######
> import Tkinter
> root = Tkinter.Tk()
> label = Tkinter.Label(root, text="hello")
> label.pack()
>
> def doCountUpdate(n):
> label['text'] = n
> label.after(1000, doCountUpdate, n + 1)
>
> doCountUpdate(0)
> root.mainloop()
> ######
>
>
> The initial call to doCountUpdate() updates the label text, and then tells
> the label: "after 1000 milliseconds (one second), call doCountUpdate
> again, and with n+1 as its argument." After we schedule this next update,
> we give control back to the GUI, and trust that the GUI will keep its
> promise to call our function after the timeout period.
>
> For reference information on this, see:
>
> http://www.pythonware.com/library/tkinter/introduction/x9507-alarm-handlers-and-other.htm
>
> This isn't the only way to handle this sort of problem: another possible
> approach involves the use of threads. But you may want to try using
> 'after()' first, since I think it has an easier learning curve than
> threads.
>
>
> Best of wishes to you!
>
More information about the Tutor
mailing list