How to get the state of a checkbox certain that was auto generated from a list

0 votes
asked Sep 11, 2017 by erik

Here's the code:

c = len(clientlist)
for x in range(1, c):
        Checkbutton(root2, text=clientlist[x], onvalue=1).grid(row=row, column=0, sticky=W)

What it looks like

Now, what I want to know, is how do I check if any of the checkboxes are checked. Since I can't figure out a way to give a dynamic variable to the checkbuttons. (Example: checkbox1, checkbox2, etc.)

Basically I just need to find a way to get the state from every generated checkbox - I can't figure it out.

1 Answer

0 votes
answered Sep 11, 2017 by jarek-d

I assume you use Tkinter. Try this:

import Tkinter as tk

chkbutton_state = {}

for chk in checkbuttons:
    var = tk.IntVar() 
    btn = Checkbutton(root2, text=chk, onvalue=1, variable=var)
    chkbutton_state[chk] = var

# render
for checkbutton in checkbuttons:
    checkbutton.grid(row=row, column=0, sticky=W)

# then somewhere else, read the state ect
print checkbutton_state
print checkbutton_state['c1'] 

And if your intention was to skip the first element of the list as the code seems suggest add after first line:

checkbuttons.pop(0)
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...