3

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()
3
  • its Checkbutton with a capital C Commented Aug 4, 2022 at 0:32
  • also you cant access the btn from in the function Commented Aug 4, 2022 at 0:33
  • You haven't packed button3 and you need to use Button3_bool.get() Commented Aug 4, 2022 at 0:40

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

I got it finally! Thank you to everyone who helped me with this question: flaxon, Derek, abnathan, & Ian! This makes a lot more sense now.
0

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.

Comments

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.