Search code examples
pythonpython-3.xtkintercheckbox

Tkinter checkboxes ticking at the same time


In this code, when you select one of the other checkboxes, the other one gets selected or deselected synchronised with each other.

For example if you clicked the aud checkbox, the mic checkbox will act as though you clicked it and both will toggle.

import tkinter as tk
root = tk.Tk()

#Themes formatted as [background, text, Window background]
theme_dark = ['#1F2140', '#989BCD', '#121426']

audiouse = 0
micuse = 0

aud = tk.Checkbutton(root, text = "Would you like audio to be used by default?", variable=audiouse)
aud.grid(row=1, column=0, columnspan=4, pady=3)


mic = tk.Checkbutton(root, text = "Would you like microphone to be used by default?", variable=micuse)
mic.grid(row=2, column=0, columnspan=4, pady=3)

root.mainloop()

When one box is clicked it is supposed to toggle on its own without affecting the other box. I have tried changing the variables and the names of the variables holding the actual boxes themselves, but nothing helped.


Solution

  • audiouse = 0 will set the variable type as <class 'int'>

    In Tkinter, such variable types will not work.

    Just setting the variables as Tk variable types will make it work correctly:

    audiouse = tk.IntVar()
    micuse = tk.IntVar()