[Tutor] Newbie question on passing a variable

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Tue, 22 Aug 2000 09:59:21 -0700 (PDT)


On Tue, 22 Aug 2000 FxItAL@aol.com wrote:

> Thanks for your response. I've brought it all together to simplify things. It loads fine but when I enter a number and push the "Action Button" I receive the following:
> Exception in Tkinter callback
> Traceback (innermost last):
>   File "D:\Python\Lib\lib-tk\Tkinter.py",line 764, in __call__
>   File "data.py",line 20, in ActionButCmd
>     while ClockMin<InputMin or ClockHour<InputHour:
> NameError: ClockMin

I actually didn't get that error message; I got a different one based on
the indentation at the very beginning of the while loop:

###
inconsistent dedent
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "/usr/tmp/python-27557Yp1", line 21
    ClockMin=Hour_Min[4]
###

That particular line also refers to "me", but should have been "time()".  
Once I corrected it, your program executed.  I'll repost it here:


###
from Tkinter import *
from time import *

root = Tk()
root.title('Top Level')

UserEntry = StringVar()
EntryBox = Entry(root, textvariable=UserEntry, width=10)
EntryBox.pack()
var = UserEntry.get()

Hour_Min=localtime(time())
ClockMin=Hour_Min[4] 
ClockHour=Hour_Min[3] 
InputMin=var
InputHour=Hour_Min[3]

def ActionButCmd():
    while ClockMin<InputMin or ClockHour<InputHour:
        Hour_Min=localtime(time())
        ClockMin=Hour_Min[4]
        ClockHour=Hour_Min[3]
    print "great"


ActionBut = Button(root, text='Action', command=ActionButCmd)
ActionBut.pack(side=RIGHT)       
QuitBut = Button(root, text='Quit', command=root.quit)
QuitBut.pack(side=LEFT)
root.mainloop()
###


You're using some global variables to pass information to your
ActionButCmd().  In order to prevent weirdness from happening, you might
want to add the following line into ActionButCmd():

###
def ActionButCmd():
    global ClockMin, InputMin, ClockHour, InputHour
    ...
###

This is to assure Python that you really do mean to use global variables,
and that they won't be localized to just ActionButCmd().



I placed a debugging statement to show the value of those variables,
because there's a bug in the program that you'll run into --- InputMin is
always the empty string!  This will help you debug your program, but to
tell the truth, I'm not sure why it's doing that.

Can any Tk/Tcl experts help with this one?  I don't know much on Tkinter
I/O, so you might want to ask some other on the tutor list..

Finally, I placed a 30 second sleep period in the while loop, just to give
my computer a rest.  Here are the debug function and
nicer-but-somewhat-buggy ActionButCmd():

###
def debugPrint(ClockMin, InputMin, ClockHour, InputHour):
    print "ClockMin:", ClockMin
    print "InputMin:", InputMin
    print "ClockHour:", ClockHour
    print "InputHour:", InputHour

def ActionButCmd():
    global ClockMin, InputMin, ClockHour, InputHour
    while ClockMin<InputMin or ClockHour<InputHour:
        Hour_Min=localtime(time())
        ClockMin=Hour_Min[4]
        ClockHour=Hour_Min[3]
        debugPrint(ClockMin, InputMin, ClockHour, InputHour)
	sleep(30) # wait for 30 seconds
    print "great"
###