faqts : Computers : Programming : Languages : Python : Snippets : Tkinter

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

20 of 20 people (100%) answered Yes
Recently 10 of 10 people (100%) answered Yes


Timed events in Tkinter (using after)
Where does 'after' come from???

Jul 5th, 2000 10:00
Nathan Wallace, unknown unknown, jean-marc deschamps, Hans Nowak, Snippet 150, Python Snippet Support Team

Packages: gui.tkinter
""" after-method.py
    Test program for the after method. Adds a random number to the
    ScrolledText box every 500 ms.
    Also shows how to make the ScrolledText box scrolls down automatically
    so we will always see the latest numbers.
from Tkinter import *
from ScrolledText import ScrolledText
import whrandom, time
class MyThing (Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
    def createWidgets(self):
        self.textBox = ScrolledText(self, height=15, width=30)
        # setting up random stuff
        whrandom.seed = time.time()
        # registering callback
        self.listenID = self.after(500, self.addnumber)
    def addnumber(self):
        number = int(whrandom.random() * 1000)
        self.textBox.insert(END, str(number) + "\n")
        self.textBox.yview("moveto", 1.0)
        self.listenID = self.after(500, self.addnumber)
mything = MyThing()