如何通过 Tkinter 中的复选框禁用按钮

How to disable a button by a checkbox in Tkinter

我正在研究 Python 的 GUI,但我不知道如何禁用带有复选按钮的按钮。 Python 使用哪个触发器来验证我是否标记了复选按钮?按照我写的一些代码尝试这样做,但没有成功。

抱歉我的英语不好。

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

你得传一个函数给command,这个函数就是每次有变化都会通知的,你可以通过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)
....

使用命令选项。您可以使用 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())

遵循包含一些单选按钮的代码来使用复选框。

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