-1

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.

Jarek.D
  • 1,274
  • 1
  • 8
  • 18
Erik
  • 1
  • 1

1 Answers1

0

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)
Jarek.D
  • 1,274
  • 1
  • 8
  • 18
  • Thank you for answering my question so quickly, even though my question might be a bit vague. I'm new to this website. But it seems to throw this error at me: `AttributeError: 'Checkbutton' object has no attribute 'state'` – Erik Sep 12 '17 at 08:35
  • Apologies, I mixed up libraries in my head. Edited my answer now - it's a bit more elaborate for Tkinter – Jarek.D Sep 12 '17 at 08:58
  • Thanks again, but it still throws me another error: `KeyError: 'c1' ` – Erik Sep 12 '17 at 12:10
  • ok, so comment out the last line and check what are the keys in the ouput of the print command above that - it prints the whole dict. I don't know exactly what's in your checkbuttons variable. Please use this as a guide – Jarek.D Sep 12 '17 at 13:16