0

So this is the code, which draws heavily from this question:

How do I create multiple checkboxes from a list in a for loop in python tkinter.

from tkinter import * 

root = Tk()

enable = {'jack': 0, 'john': 1} 

def actright(x):
    print(enable.get(x))

for machine in enable:
    enable[machine] = Variable()
    l = Checkbutton(root, text=machine, variable=enable[machine], 
                    command=  actright(machine))
    l.pack(anchor = W)

root.mainloop()

I expected the output to be:

0
1

Instead the output is:

PY_VAR0
PY_VAR1

How can I get these values without the "PY_VAR" preceding the number?

1 Answer 1

1

remove the enable[machine] = Variable()

for machine in enable:
    l = Checkbutton(root, text=machine, variable=enable[machine],
                    command=  actright(machine))
    l.pack(anchor = W)

root.mainloop()

You see PY_VAR0 and PY_VAR1 because you set the values to those with enable[machine] = Variable(), that overwrites the values in your dict so it makes sense that you get the output you do.

Sign up to request clarification or add additional context in comments.

1 Comment

No worries, happy tkintering!

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.