Command config, quitting, binary, Timer

Diez B. Roggisch deets at nospam.web.de
Sun Sep 4 07:01:14 EDT 2005


> ! import Tkinter
> ! def dogo():
> !     while 1:
> !         b.config(command=lambda:None)
> ! root = Tkinter.Tk()
> ! b = Tkinter.Button(root, text="Go", command=dogo)
> ! b.pack()
> ! root.mainloop()


I guess tkinter has to keep a name-reference pair (some days a 
discussion about this arose and /F said something like that). It 
certainly is somewhat ugly - but there might be very good reasons to do so.
> 
> Note: I have found this problem because in a small program I have a
> Start button that becomes a Stop, so the bound command must change each
> time. (Maybe there is some other solution, like hiding/unhiding the
> buttons, I don't know.)

Had that to do myself - my solution was to use the buttons text for 
swich-casing. I guess you change the buttons text anyways (otherwise 
nobody would know that its semantic changed), so do something like this 
(untetsted)

def start_stop_callback():
     if b.get_config("text") == "Start":
        b.config(text="Stop")
        ....
     else:
        b.config(text="Start")
        ....


> Sometimes I need something like the Timer of Java, it generates "ticks"
> and calls a function for each tick. The tick frequency can be set, or
> they can be stopped.
> 
> In another Newsgroup someone has suggested me that the callLater of
> Twisted can solve my problem, but I think it's a quite common thing, so
> I think that maybe it can be added to the threading standard module.
> 
> This is a rough Python version of mine (without comments), it's not a
> true Metronome because it counts the delay time after the end of the
> last function call. This class also seems fragile, sometimes it gives
> me problems, and I cannot use too much concurrent metronomes, etc. It's
> quite
> 
> Maybe someone can suggest me how to improve it.
> 

Instead of several metronomes, use one thread and compute the gresatest 
common divisor for all scheduled timings. Use that as sleep interval 
(your "ticks"), and count ticks for each timer. You should introduce a 
lower thrshold on tick-length, otherwise you can get into trouble.


Diez



More information about the Python-list mailing list