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.

Recommended Answers

All 2 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.