0

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:

Error:
Traceback (most recent call last):
File "C:/Documents and Settings/Some_Kittens/Desktop/Lab20/worldClock.py", line 33, in <module>
main()
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

Code:

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
        vars()[button].activate()
        
    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()

    
    win.close()

main()

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

Edited by SomeKittens: n/a

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by Gribouillis
0

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 by woooee: n/a

0

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 by Gribouillis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.