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?

Community
  • 1
  • 1

1 Answers1

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.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321