4

I'm studying GUI for Python, and I don't know how to disable a button with a check button. Which trigger does Python use to verify if I mark the check button? Follow some code I wrote trying to do it, but without success.

Sorry for my bad English.

from tkinter import *
from tkinter import ttk


class HelloApp:

    def __init__(self, master):

        self.label = ttk.Label(master, text="Hello, Tkinter!")

        self.button1 = ttk.Button(master, text="Texas", command=self.texas_hello)
        self.button2 = ttk.Button(master, text="Hawaii", command=self.hawaii_hello)

        value_check = IntVar()

        def disable_button(button):
            button.config(state=DISABLED)

        def enable_button(button):
            button.config(state=NORMAL)

        checkbutton = ttk.Checkbutton(master, variable=value_check, text='Deactivate!',
                                      onvalue=enable_button(self.button1),
                                      offvalue=disable_button(self.button1))

        self.label.grid(row=0, column=0, columnspan=2)
        self.button1.grid(row=1, column=0)
        self.button2.grid(row=1, column=1)
        checkbutton.grid(row=1, column=2)

        print(value_check)

    def texas_hello(self):
        self.label.config(text='Howdy, Tkinter!')

    def hawaii_hello(self):
        self.label.config(text='Aloha, Tkinter!')


def main():

    root = Tk()
    HelloApp(root)
    root.mainloop()


if __name__ == "main": main()

main()
Alcides Neto
  • 43
  • 1
  • 6

3 Answers3

2

You have to pass a function to command, this function is the one that will be notified every time there is a change, and you can get the status through value_check.

...
value_check = IntVar()

def disable_enable_button(button):
    self.button1.config(state=DISABLED if value_check.get() else NORMAL)

checkbutton = ttk.Checkbutton(master, variable=value_check, text='Deactivate!',
                              command=disable_enable_button)
....
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you so much for the help. I use part of this code to play with radio buttons and the check button. Now I can select which button to disable due to the radio button I choose. – Alcides Neto Apr 09 '18 at 13:51
  • @AF.Neto If my answer helped you do not forget to mark it as correct, that is the best way to thank. : D – eyllanesc Apr 10 '18 at 04:45
  • Thanks @eyllanesc I changed `def disable_enable_button():` to get this to work – Alexx Roche Jan 16 '19 at 13:35
0

use command option. you can set the default status using value_check.set(1/0).

def disable_button(self, button):
    print('disable button')
    button.config(state=DISABLED)

def enable_button(self, button):
    print('enable button')
    button.config(state=NORMAL)

def changebutton(self):
    print('changebutton=', self.value_check.get())
    if self.value_check.get()==1:
        self.enable_button(self.button1)
    else:
        self.disable_button(self.button1)

def __init__(self, master):

    self.label = ttk.Label(master, text="Hello, Tkinter!")

    self.button1 = ttk.Button(master, text="Texas", command=self.texas_hello)
    self.button2 = ttk.Button(master, text="Hawaii", command=self.hawaii_hello)

    self.value_check = IntVar()
    self.checkbutton = ttk.Checkbutton(master, variable=self.value_check, text='Activate!',
                                    onvalue=1, offvalue=0,
                                    command=self.changebutton)
    self.value_check.set(0)
    self.changebutton()

    self.label.grid(row=0, column=0, columnspan=2)
    self.button1.grid(row=1, column=0)
    self.button2.grid(row=1, column=1)
    self.checkbutton.grid(row=1, column=2)
    print(self.value_check.get())
Junhee Shin
  • 748
  • 6
  • 8
0

Follows the code including some radio buttons to play with the checkbox.

class HelloApp:

    def __init__(self, master):

        self.label = ttk.Label(master, text="Hello, Tkinter!")

        self.button1 = ttk.Button(master, text="Texas", command=self.texas_hello)
        self.button2 = ttk.Button(master, text="Hawaii", command=self.hawaii_hello)

        self.value_check = IntVar()
        self.value_check.set(0)

        self.checkbutton = ttk.Checkbutton(master, variable=self.value_check, text='Activate!',
                                           onvalue=1, offvalue=0,
                                           command=self.disable_enable_button)

        self.choice = StringVar()

        self.frame_radio = ttk.Frame(master).grid(row=3, column=3)
        self.radiobutton1 = ttk.Radiobutton(self.frame_radio, text='Button 1', variable=self.choice, value='button1')
        self.radiobutton1.grid(row=2, column=2)
        self.radiobutton2 = ttk.Radiobutton(self.frame_radio, text='Button 2', variable=self.choice, value='button2')
        self.radiobutton2.grid(row=3, column=2)

        self.label.grid(row=0, column=0, columnspan=2)
        self.button1.grid(row=1, column=0)
        self.button2.grid(row=1, column=1)
        self.checkbutton.grid(row=1, column=2)

    def texas_hello(self):
        self.label.config(text='Howdy, Tkinter!')

    def hawaii_hello(self):
        self.label.config(text='Aloha, Tkinter!')

    def disable_enable_button(self):
        self.button1.config(
            state=DISABLED if self.value_check.get() and self.choice.get() == 'button1' else NORMAL)
        self.button2.config(
            state=DISABLED if self.value_check.get() and self.choice.get() == 'button2' else NORMAL)

    def main():

       root = Tk()
       HelloApp(root)
       root.mainloop()
WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
Alcides Neto
  • 43
  • 1
  • 6