[Tutor] Help passing parameters to cancel callback (after_cancel)

dkstewaP@netscape.net dkstewaP@netscape.net
Sun, 01 Apr 2001 03:41:26 -0400


I am trying to build a demonstration/test module for process control application using Tkinter. I want to include a clock updating in one second intervals that can be started and stopped by the user (effictively a stop watch type application). My previous programming experience has been in various flavours of basic including VBA for quite complex applications in msWord and Excel. 

I am very confused as to how I pass the id parameter from the call to after() to the after_cancel() function. I think my problems are related to the scope of variables (namespaces in python) and how to refer to the variable in the various namespaces. The combination of using a class to define my functions and having to pass additional namespace info to Tkinter has really got me beat.

My sample code follows (adapted from the hello word example in "An Introduction to Tkinter"). Any help to fix the code and to help me understand Python would be gratefully appreciated.

David
Brisbane, Australia

# File: hello2.py
"""Display System Time String - potential GUI for stop watch or similar module

    Stop button should cleanly break out of the polling loop"""

from Tkinter import *
from time import *

class App:

    def __init__(self, master):

        self.idn = 0
        self.master = master
        frame = Frame(master, width = 512 , height = 256,)
        frame.pack()

        self.label = Label(frame, text="The system time is") 
        self.label.place(relx=0.1, rely=0.2)

        self.label2 = Label(frame, text=" ", width = 36 , height = 1, relief=RAISED) 
        self.label2.place(relx=0.5, rely=0.2)
        
        self.button = Button(frame, text="STOP", fg="red", command=self.stop(self.idn))
        self.button.place(relx=0.5, rely=0.8)

        self.hi_there = Button(frame, text="Quit", command=sys.exit)
        self.hi_there.place(relx=0.7, rely=0.8)

        self.idn = self.poll() # start polling

    def stop(self, idn):
        self.master.after_cancel(idn)

    def poll(self):
        count = time()
        self.label2.configure(text=str(count))
        nn  = self.master.after(1000, self.poll)
        return nn

root = Tk()
app = App(root)
root.mainloop()



  



__________________________________________________________________
Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/