Home > Mobile >  How can I check two python tkinter checkButtons when I check another checkButton?
How can I check two python tkinter checkButtons when I check another checkButton?

Time:08-04

I am new to tkinter & am having trouble with checkButton(). I want it so that when I check "Button 3", "Button 1" & "Button 2" will be checked also. This is my code so far. I looked at this question: Can i make one checkbutton in tkinter check all the other checkbuttons?, which led me to use the .select() method, but when I run the GUI & check button 3, button 1 & 2 do not get checked. How should I write the function so that when I check "Button 3", "Button 1" & "Button 2" are checked also?

from tkinter import *

root = Tk()

button1_bool = BooleanVar()
button1 = checkButton(root, text = "Button 1", variable = button1_bool, onvalue = True, offvalue = False)
button1.pack()

button2_bool = BooleanVar()
button2 = checkButton(root, text = "Button 2", variable = button2_bool, onvalue = True, offvalue = False)
button2.pack()

def button3ischecked():
    if button3.get() == True:
        button1.select()
        button2.select()

button3_bool = BooleanVar()
button3 = checkButton(root, text = "Button 3", variable = button3_bool, onvalue = True, offvalue = False, command = button3ischecked)

root.mainloop()

CodePudding user response:

You don't use .get() to inspect the state of the checkbox - just evaluate the boolean variable assigned to it.

from tkinter import *

root = Tk()

button1_bool = BooleanVar()
button1 = Checkbutton(root, text = "Button 1", variable = button1_bool, onvalue = True, offvalue = False)
button1.pack()

button2_bool = BooleanVar()
button2 = Checkbutton(root, text = "Button 2", variable = button2_bool, onvalue = True, offvalue = False)
button2.pack()

def button3ischecked():
    if button3_bool:
        button1.select()
        button2.select()

button3_bool = BooleanVar()
button3 = Checkbutton(root, text = "Button 3", variable = button3_bool, onvalue = True, offvalue = False, command = button3ischecked)
button3.pack()

root.mainloop()

Hope this helps.

CodePudding user response:

the main problems in your code

  1. use the correct Function to create a check box Checkbutton()
  2. the get() fucntion need to run on your button3_bool which is the varieble that keep the state, or you dont have to even use get() like others said
from tkinter import *

root = Tk()

button1_bool = BooleanVar()
button1 = Checkbutton(root, text = "Button 1", variable = button1_bool, onvalue = True, offvalue = False)
button1.pack()

button2_bool = BooleanVar()
button2 = Checkbutton(root, text = "Button 2", variable = button2_bool, onvalue = True, offvalue = False)
button2.pack()

def button3ischecked():
    if button3_bool.get() == True:
        button1.select()
        button2.select()

button3_bool = BooleanVar()
button3 = Checkbutton(root, text="Button 3", variable=button3_bool, onvalue=True, offvalue=False,
                      command=button3ischecked)
button3.pack()

if __name__ == '__main__':
    root.mainloop()

i would go with a class approche so you have access to everything

import tkinter as tk


class App(tk.Tk):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.button1_bool = tk.BooleanVar()
        self.button1 = tk.Checkbutton(self, text="Button 1", variable=self.button1_bool, onvalue=True, offvalue=False)
        self.button1.pack()

        self.button2_bool = tk.BooleanVar()
        self.button2 = tk.Checkbutton(self, text="Button 2", variable=self.button2_bool, onvalue=True, offvalue=False)
        self.button2.pack()

        self.button3_bool = tk.BooleanVar()
        self.button3 = tk.Checkbutton(self, text="Button 3", variable=self.button3_bool, onvalue=True, offvalue=False,
                                      command=self.button3ischecked)
        self.button3.pack()

    def button3ischecked(self):
        if self.button3_bool.get() == True:
            self.button1.select()
            self.button2.select()


if __name__ == '__main__':
    app = App()
    app.mainloop()

CodePudding user response:

Like @flaxon said, you must use Checkbutton with a capital C. Also, in the button3ischecked function, instead of checking if button3.get() == True, check if button3_bool.get() == True (you can actually just shorten that line to if button3_bool.get(): since it already is a boolean). Finally, remember to call button3.pack() to make it show up on the screen.

  • Related