1

I've got a radio button which lets me select 0, 1 or 2. The following script prints 0, 1 or 2 in the terminal, but I want it to do 'stuff' depending on what I select.

For example, If I select 0, I want it to do a root.destroy() If I select 1, I want it to do something else etc.

I thought I'd need to declare a global variable, but it doesn't seem to work.

Here is the script.

root = Tk()

v = IntVar()
v.set(0)  # set to 0 pics as default. Just looks prettier...

languages = [
    ("0 Pics",0),
    ("1 Pic",1),
    ("2 Pics",2),
]

def ShowChoice():
    world = v.get()
    global world
    print world

Label(root, text="""How many pictures do you have left to scan?""", padx = 15).pack()

for txt, val in languages:
    Radiobutton(root, 
                text=txt,
                padx = 20, 
                variable=v, 
                command=ShowChoice,
                value=val).pack(anchor=W)

mainloop()

Radiobutton(root, 
            text=txt,
            indicatoron = 0,
            width = 20,
            padx = 20, 
            variable=v, 
            command=ShowChoice,
            value=val).pack(anchor=W)


print 'The V get variable is: ' + world()

1 Answer 1

2

You can use lambda to pass your function with argument.

for txt, val in languages:
    Radiobutton(root, 
                text=txt,
                padx = 20, 
                variable=v, 
                command=lambda: ShowChoice(root),
                value=val).pack(anchor=W)

In the ShowChoice function, just use if/else to do 'stuff' depending on what is selected. For example.

def ShowChoice(root):
    world = v.get()
    if world == 0:
       root.destroy()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.