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

发布于 2024-12-10 10:00:18 字数 1714 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

负佳期 2024-12-17 10:00:18

您还没有提供关键信息——错误发生在哪一行。

但是,这里似乎存在缩进错误:

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() 行的位置。

You haven't provided the crucial info -- what line the error happens on.

However, you appear to have an indentation error here:

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")

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:

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()

That appears to be what you're trying to do. Observe the indentation closely and also where I moved the v = IntVar() line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文