Skip to content Skip to sidebar Skip to footer

Python 2.7 Using Tkinter -all Checkbox Are Being Checked When Click On One Only

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 be

Solution 1:

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 inrange(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()

Post a Comment for "Python 2.7 Using Tkinter -all Checkbox Are Being Checked When Click On One Only"