1

I would like to get status of all defined checkbuttons by "for" function. I have four checkbuttons and output is (off, off, off, off):

[0, 0, 0, 0]

But I need to have for example (off, on, on, off):

[0, 1, 1, 0]

It looks like below code is geeting only staus from last "D" checkbuton and append to "buttons_status" list.

Any idea how to get status of all chcekbuttons? Thanks in advance.

Here is a code:

from tkinter import *

names = ['A','B','C','D']
buttons_status = []

root = Tk()
for x in range(0,len(names)):
    checkbutton_input = IntVar()
    checkbutton = Checkbutton(root, text=str(names[x]),
    variable=checkbutton_input)
    checkbutton.grid(row=3, column=x)
    status = checkbutton_input.get()
    buttons_status.append(status)
root.mainloop()

print(buttons_status)
1
  • You're getting the status about a millisecond after you create the checkbutton. The user won't have had a chance to click on it. Commented Jul 6, 2018 at 17:39

1 Answer 1

1

You need to add the actual IntVars to the list and call the get methods when you want to see the status. We would usually put a functionality like that in a function:

from tkinter import *

def get_all():
    return [x.get() for x in buttons_status]

names = ['A','B','C','D']
buttons_status = []

root = Tk()
for x in range(0,len(names)):
    checkbutton_input = IntVar()
    checkbutton = Checkbutton(root, text=str(names[x]), variable=checkbutton_input)
    checkbutton.grid(row=3, column=x)
    buttons_status.append(checkbutton_input)
root.mainloop()

print(get_all())

That said, it really sounds like what you need is a subclass. What's your overall goal here?

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

1 Comment

Novel. Overall goal: I will create GUI with checkbuttons. Based on which checkbutton user select next actions will be taken. To do this I need a list with checkbutton status.

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.