Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6706559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:32:05+00:00 2026-05-26T07:32:05+00:00

I am trying to learn how to use a dictionary with a radio button.

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:32:05+00:00Added an answer on May 26, 2026 at 7:32 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn and use html5 and have a basic layout but
I'm trying to learn how to use managed/unmanaged code interop, but I've hit a
I am trying to learn CodeIgniter to use for a shopping site, but I
I'm trying to learn how to use actionscript over mxml for flexibility. I have
I am trying to learn how to use Cascadding Style Sheets. I have a
I'm trying to learn to use awk but it's not behaving how I expect.
I have been trying to learn to use github, it has been fine until
I'm trying to learn how to use the Control.Parallel module, but I think I
I'm trying to learn to use ANTLR, and seem to have come across an
I am currently trying to learn how to use easymock. I have the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.