Xartab
Xartab

Reputation: 37

Tkinter problem: checking one checkbox checks all of them

I'm writing a little program that needs the user to decide some boolean values. I've made checkboxes to handle this part, but the problem is that every time I check or uncheck one, all the others follow along.

I've searched online, but the only explanation I found (python 2.7 using tkinter -all checkbox are being checked when click on one only) doesn't seem to apply in my case.

import tkinter as tk

''' Init '''
variable1 = True
variable2 = True
variable3 = True

''' Set window '''
window = tk.Tk()
window.title('Title')
window.geometry('600x400')

''' Contents '''
check1 = tk.Checkbutton(window, text="Sometext_1", variable=variable1, onvalue=True, offvalue=False, height=2)
check2 = tk.Checkbutton(window, text="Sometext_2", variable=variable2, onvalue=True, offvalue=False, height=2)
check3 = tk.Checkbutton(window, text="Sometext_3", variable=variable3, onvalue=True, offvalue=False, height=2)

''' Show '''
check1.pack()
check2.pack()
check3.pack()


''' Window loop '''
window.mainloop()

Seems like the checkboxes should be checked independently, instead they all check and uncheck as one. Any idea would be appreciated.

Upvotes: 1

Views: 899

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386382

The value of the variable attribute must be instances of one of the special tkinter variables StringVar, Intvar, DoubleVar, or BooleanVar.

variable1 = tk.BooleanVar(value=True)
variable2 = tk.BooleanVar(value=True)
variable3 = tk.BooleanVar(value=True)

Note: you can only create these variables after creating the root window.

Upvotes: 3

Related Questions