0

I'm using python 2.7 with Tkinter (new to Tkinter:)) I have UI with list of 20 checkboxes once I click on one checkbox, all checkboxes are being checked, instead of one. In code below you'll see 2 line (once with #) with # -when click on checkbox only one is checked which is ok without # -when click on one, all are being checked The problem is that I want to know the status of each checkbox if checed or not and I have to define var=Intvar in order to "get" it status Any suggestion? Thanks in advance Below is relevant def

def suites_checkbox_create(self):
    ExcelWorkBook1 = open_workbook(config.UI_Suites_Location + 'STD_SUITES.xlsx', on_demand=True)
    First_Sheet1 = ExcelWorkBook1.sheet_by_index(0)
    plushight = 110
    suitesList=[]
    self.CheckboxList=[]
    for name in (First_Sheet1._cell_values):
        if name[3] == "General Name":
            continue
        else:
            suitesList.append(name[3])


    for index, name in enumerate(suitesList):

            self.var=IntVar
            #self.CheckboxList.append(Checkbutton(self.app, text=name)) # using this, i can check once checkbox a time
            self.CheckboxList.append(Checkbutton(self.app, text=name, variable=self.var)) # with this, once i check once checkbox, all checkboxes(20) are bing checked
            self.CheckboxList[index].place(y=plushight)
            plushight += 20
1
  • 2
    You have forgotten the brackets for IntVar and if you want to be able to get the state of each checkbutton, you need to put the IntVars in a list like for the checkbuttons. Commented Oct 25, 2017 at 8:53

1 Answer 1

3

The reason this happens is because you've given all of your Checkbutton widgets the same variable for their variable attribute.

Meaning that as soon as one of the Checkbutton widgets is ticked self.var is given a value of 1 which means that all of the Checkbutton widgets have a value of 1 which equates to them having been selected.

In short, whenever one is ticked it updates the value of all the other's because they have the same variable used to store their value.

See this in the example below:

from tkinter import *

root = Tk()
var = IntVar()

for i in range(10):
    Checkbutton(root, text="Option "+str(i), variable = var).pack()

root.mainloop()

To resolve this you need to use a different variable for each Checkbutton, like the below:

from tkinter import *

root = Tk()
var = []

for i in range(10):
    var.append(IntVar())
    Checkbutton(root, text="Option "+str(i), variable = var[i]).pack()

root.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

You will also have this same issue if your Variables are not set to be an IntVar(). You must assign them to be IntVars like so: checkboxvar = IntVar(). Also note that to get the value of the checkbox, you must do checkboxvar.get()

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.