Reputation: 478
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()
Upvotes: 0
Views: 199
Reputation: 176780
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.
Upvotes: 1