我正在学习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()
答案 0 :(得分:2)
您必须将一个函数传递给命令,此函数是每次更改时都会收到通知的函数,您可以通过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)
....
答案 1 :(得分:0)
使用命令选项。您可以使用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())
答案 2 :(得分:0)
按照包含一些单选按钮的代码来使用复选框。
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()