I'm trying to convert text to a variable name. So far, I've been able to solve most things with Google, but this is really frustrating:

Traceback (most recent call last):
File "C:/Documents and Settings/Some_Kittens/Desktop/Lab20/worldClock.py", line 33, in <module>
File "C:/Documents and Settings/Some_Kittens/Desktop/Lab20/worldClock.py", line 21, in main
while not Quit.clicked(pt):
NameError: global name 'Quit' is not defined


from graphics import *
from clock import Clock
from button import Button

def main():
    win = GraphWin("World Clock", 200, 250)

    worldClock = Clock(win, Point(5,5), Point(200,200))
    buttons = ["EST", "UTC", "PST", "Quit"]
    xvar = 25
    for button in buttons:
        vars()[button] = Button(win, Point(xvar,220), 40, 20, button)
        xvar = xvar + 50
    pt = win.getMouse()
    while not Quit.clicked(pt):
        if EST.clicked(pt):
            worldClock.showCurrentTime(-5, "EST")
        if UTC.clicked(pt):
            worldClock.showCurrentTime(0, "UTC")
        if PST.clicked(pt):
            worldClock.showCurrentTime(-8, "PST")        
        pt = win.getMouse()



The graphics window shows up fine, so I know "Quit" is being defined.

Edited 6 Years Ago by SomeKittens: n/a

You don't have any button or other widget variable "Quit". You possibly want to store the memory address for the buttons in a list and then use button_list[3].clicked() as it is the last/fourth button created. Using the local symbol table is somewhat obfuscated code IMHO.

Edited 6 Years Ago by woooee: n/a

One way to do this is to write

exec ";".join(b+"=None" for b in buttons)

immediately after line 9.

However, it's a bad programming style. You should better use a dict, a list or a class instance.

Edited 6 Years Ago by Gribouillis: n/a

This article has been dead for over six months. Start a new discussion instead.