如何修复代码中的语法以使单选按钮与字典一起使用?
我正在尝试学习如何使用带有单选按钮的字典。我有下面的代码,但是当我运行它时,我收到错误。
错误说:
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()
I am trying to learn how to use a dictionary with a radio button. I have the code below but when ever I run it I get an error.
The error says:
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
Here's my code:
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还没有提供关键信息——错误发生在哪一行。
但是,这里似乎存在缩进错误:
for
循环内的行需要缩进。如果修复此问题不能解决您的问题,请对我的答案发表评论,并使用行号和已修复缩进的代码更新您的问题。
更新问题的更新答案:
尝试此代码:
这似乎就是您想要做的。仔细观察缩进以及我移动
v = IntVar()
行的位置。You haven't provided the crucial info -- what line the error happens on.
However, you appear to have an indentation error here:
The lines inside the
for
loop need to be indented.If fixing that doesn't fix your problem please leave a comment on my answer and update your question with the line number and the code with the indentation fixed.
Updated answer for the updated question:
Try this code:
That appears to be what you're trying to do. Observe the indentation closely and also where I moved the
v = IntVar()
line.