[Tutor] after(), how do I use it?

Edgar Almonte samudhio at gmail.com
Tue Apr 26 03:22:37 CEST 2011


after(delay_ms, callback=None, *args) [#]

    Registers an alarm callback that is called after a given time.

    This method registers a callback function that will be called
after a given number of milliseconds. Tkinter only guarantees that the
callback will not be called earlier than that; if the system is busy,
the actual delay may be much longer.

    The callback is only called once for each call to this method. To
keep calling the callback, you need to reregister the callback inside
itself:

    class App:
        def __init__(self, master):
            self.master = master
            self.poll() # start polling

        def poll(self):
            ... do something ...
            self.master.after(100, self.poll)

    after_cancel to cancel the callback.


On Mon, Apr 25, 2011 at 9:15 PM, Wayne Werner <waynejwerner at gmail.com> wrote:
> On Mon, Apr 25, 2011 at 8:02 PM, michael scott <jigenbakuda at yahoo.com>
> wrote:
>>
>> Here is the code in its entirety, it works although I don't see after()
>> defined, so I assumed it was a built in function. I can just copy and paste
>> parts of this code into my project, so its not imperative that I understand,
>> but I prefer to use the weapons I've been given. So I hope that you guys can
>> understand it a bit better after I post this.
>
> That it is indeed. Do you understand classes and subclassing in Python? App
> is a subclass of tk.Tk. If you have done any of your own programming in
> Tkinter, you should know that Tk is the "main" class in Tkinter. If you do a
> Google search for "Tkinter after", the top two results will answer your
> question:
> http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=tkinter+after
> HTH,
> Wayne
>
>>
>> import Tkinter as tk
>>
>> class App(tk.Tk):
>>     def __init__(self,*args, **kwargs):
>>         tk.Tk.__init__(self, *args, **kwargs)
>>         self.label = tk.Label(self, text="", width=20, anchor="w")
>>         self.label.pack(side="top",fill="both",expand=True)
>>         self.print_label_slowly("Hello, world!")
>>
>>     def print_label_slowly(self, message):
>>         '''Print a label one character at a time using the event loop'''
>>         t = self.label.cget("text")
>>         t += message[0]
>>         self.label.config(text=t)
>>         if len(message) > 1:
>>             self.after(500, self.print_label_slowly, message[1:])
>>
>> app = App()
>> app.mainloop()
>>
>>
>> ----
>> What is it about you... that intrigues me so?
>>
>> ________________________________
>> From: Adam Bark <adam.jtm30 at gmail.com>
>> To: tutor at python.org
>> Sent: Mon, April 25, 2011 8:50:16 PM
>> Subject: Re: [Tutor] after(), how do I use it?
>>
>> On 26/04/11 01:36, michael scott wrote:
>> > Hello, I asked for help in another location and it solved my problem,
>> > but the only problem is I don't fully understand the after function. Here is
>> > part of the code that was given to me.
>> >
>> >
>> >    def print_label_slowly(self, message):
>> >        '''Print a label one character at a time using the event loop'''
>> >        t = self.label.cget("text")
>> >        t += message[0]
>> >        self.label.config(text=t)
>> >        if len(message) > 1:
>> >            self.after(500, self.print_label_slowly, message[1:])
>> >
>> > I understand it, and the gist of how it works, but the self.after... I
>> > can not find any documentation on it (because after is such a common word),
>> > so can you guys tell me how it works. Is this a built in function (didn't
>> > see it on the built in function list)? Does it always take 3 arguements? Is
>> > this a user made function and I'm just overlooking where it was defined at?
>>
>> The function you have shown there appears to be a class method. self.after
>> means you are calling another method of the same function that
>> print_label_slowly is a part of.
>> Do you have the rest of the code? If you're still confused post it and
>> hopefully we can clear it up for you.
>>
>> HTH,
>> Adam.
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


More information about the Tutor mailing list