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 9115515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:27:06+00:00 2026-06-17T04:27:06+00:00

I am new to python and have a task to take input from a

  • 0

I am new to python and have a task to take input from a listbox and create radiobuttons for each entry. In my code I am able to create the radio buttons but they don’t function when I click on them i.e. in this case they don’t print “hello” and the number i. Here’s the code:

def generateGraph():
    w = Toplevel(bg = "grey")
    w.resizable(0,0)
    frameData = Frame(w, bg="grey", padx=10, pady=10)
    frameData.grid(row = 0, column=0, pady = 1, padx = 1, sticky = N+E+S+W)
    InputLabel = Label(frameData, text="Inputs:", bg="grey")
    InputLabel.grid(row=1, column=0, padx=10, sticky=N+E+S+W)
    OutputLabel = Label(frameData, text="Outputs:", bg="grey")
    OutputLabel.grid(row=1, column=1, padx=10, sticky=N+E+S+W)

    i=0
    c=[]
    inputVar = IntVar()
    while(InputBox.get(i)):
        c.append(Radiobutton(frameData, text=InputBox.get(i), variable=inputVar, value = i, background="grey", command= hello(i)))
        c[i].grid(row = i+2, column = 0, sticky = W)
        i=i+1
    if makemodal:
        w.focus_set()
        w.grab_set()
        w.wait_window()
def hello(i):
    print("hello %d" %i)

Please help and thanks in advance.

  • 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-06-17T04:27:07+00:00Added an answer on June 17, 2026 at 4:27 am

    The problem is that you’re calling hello(i) at the time of construction of the Radiobutton, not storing something to be called later:

        c.append(Radiobutton(frameData, text=InputBox.get(i), variable=inputVar, value = i, background="grey", command= hello(i)))
    

    Since hello returns None, you’re effectively storing command=None.

    You need to store a callable here, like hello itself, (or a lambda or partial, or whatever), not the result of calling it.

    For example:

        c.append(Radiobutton(frameData, text=InputBox.get(i), 
                             variable=inputVar, value = i, background="grey",
                             command=functools.partial(hello, i)))
    

    Since you asked in the comments: Note that I used partial rather than lambda, because I want to bind in the value of i, not close over the variable i. Otherwise, you’d end up with, e.g., 5 radio buttons all bound to the same variable i with the value 4. There are other ways around this—use an explicit factory, do lambda x=i: hello(x) instead of lambda: hello(i), etc. To me, partial seems like the clearest and most explicit, but your mileage may vary. Anyway, there are dozens of questions on SO about this, but the answer to Scope of python lambda functions and their parameters seems particularly clear.

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

Sidebar

Related Questions

I am new to python. I have a task to access wikipedia. For this
I am still new to Python and have been reviewing the following code not
I am new to Python and have been studying its fundementals for 3 months
I'm new to python and have hit a problem with an SQL query I'm
hello im new to python and have been reading over the documentation and am
I am very new to python programming and have yet to buy a textbook
I am very new to Python and I have a json news feed that
I am new to python and I have a serious problem that I cannot
I'm new to Python and I have been trying to search through html with
I'm a new to Python. I have installed Python 2.7 64 bit on Win7

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.