如何修复代码中的语法以使我的单选按钮与我的字典一起使用?

时间:2011-10-17 03:30:29

标签: python dictionary radio-button tkinter

我正在尝试使用单选按钮学习如何使用词典。我有下面的代码,但是当我运行它时,我得到一个错误 错误说:

Traceback (most recent call last):   File "/Volumes/CHROME
USB/STORAGE/TKinker GUI/Radiobutton + Dictionary.py", line 16, in
<module>
    for i in sorted(choices.keys()): NameError: name 'choices' is not defined

这是我的代码:

from Tkinter import *
import time

class App:
    def __init__(self, master):
        w = Label(master, text="1. Anxiety, nervousness, worry or fear")
        w.pack()

    choices = {
    1: "not at all",
    2: "somewhat",
    3: "moderately",
    4: "a lot"
    }

for i in sorted(choices.keys()):
    label = "%s - %s" % (i, choices[i])
    rb=Radiobutton(master, text=label, variable=v, value=i)
    rb.pack(side=TOP, anchor="w")

    choices = {
    1: "not at all",
    2: "somewhat",
    3: "moderately",
    4: "a lot"
    }

    v = IntVar()
    Radiobutton(master, text="0 for not at all", variable=v, value=1).pack(side=TOP, anchor="w")
    Radiobutton(master, text="1 for somewhat", variable=v, value=2).pack(side=TOP, anchor="w")
    Radiobutton(master, text="2 for moderatly", variable=v, value=3).pack(side=TOP, anchor="w")
    Radiobutton(master, text="3 for a lot", variable=v, value=4).pack(side=TOP, anchor="w")

    self.button = Button(master, text="BACK", fg="red", command=self.button6)
    self.button.pack(side=BOTTOM)
    self.button = Button(master, text="NEXT", fg="red", command=self.button5)
    self.button.pack(side=BOTTOM)

    def button6(self):
        print "Sam is awesome!GAJONGA" 

    def button5(self):
        print "PYTHON FOR THE WIN! GIAN SAYS PYTHON = FILTHY" 

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

1 个答案:

答案 0 :(得分:1)

您尚未提供关键信息 - 错误发生在哪一行。

但是,您似乎在此处出现缩进错误:

for i in sorted(choices.keys()):
    label = "%s - %s" % (i, choices[i])
    rb=Radiobutton(master, text=label, variable=v, value=i)
    rb.pack(side=TOP, anchor="w")

for循环内的行需要缩进。

如果修复无法解决您的问题,请对我的回答发表评论,并使用行号和修补后的代码更新您的问题。


更新了更新问题的答案:

试试这段代码:

from Tkinter import *
import time

class App:
    def __init__(self, master):
        w = Label(master, text="1. Anxiety, nervousness, worry or fear")
        w.pack()

        choices = {
        1: "not at all",
        2: "somewhat",
        3: "moderately",
        4: "a lot"
        }

        for i in sorted(choices.keys()):
            v = IntVar()
            label = "%s - %s" % (i, choices[i])
            rb=Radiobutton(master, text=label, variable=v, value=i)
            rb.pack(side=TOP, anchor="w")

        self.button = Button(master, text="BACK", fg="red", command=self.button6)
        self.button.pack(side=BOTTOM)
        self.button = Button(master, text="NEXT", fg="red", command=self.button5)
        self.button.pack(side=BOTTOM)

    def button6(self):
        print "Sam is awesome!GAJONGA"

    def button5(self):
        print "PYTHON FOR THE WIN! GIAN SAYS PYTHON = FILTHY"

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

这似乎是你想要做的。仔细观察缩进以及我移动v = IntVar()行的位置。