python - 如何复选框以启用 Tkinter 中的按钮
问题描述
self.label_5 = tk.Checkbutton(self.master, text="I agree to the", bg='white', width=14,font=("Arial", 8), command= activator)
self.label_5.place(x=112, y=410)
self.button_2 = tk.Button(text='Proceed', width=20, bg='white', state = tk.DISABLED, bd=1,
highlightbackground='black', font=("Arial", 10)).place(x=208, y = 512)
def activator(button):
if (self.button_2 ['state'] == tk.DISABLED):
self.button_2 ['state'] = tk.NORMAL
else:
self.button_2['state'] = tk.DISABLED
我想在检查复选按钮后启用继续按钮,但我似乎无法弄清楚。
解决方案
您必须对代码进行以下更改:
- 将函数命名
activator
为 asself.activator
时,必须将其引用为Button
(button_2
)command
。 - 您必须将
parameter
名为button
的函数的名称更改activator
为self
. - 您需要做的最重要的事情是将放置
Button
(button_2
) 和Checkbutton
(label_5
) 的代码部分移到新行。就像我在下面的代码中所做的那样。这样做的原因是pack
,grid
而且place
总是如此return
None
。当您在创建小部件并将它们分配给变量的同一行中执行此操作时,即button_2
andlabel_5
,该值None
将存储在该小部件中。
这是更正后的代码:
import tkinter as tk
class Test:
def __init__(self):
self.master = tk.Tk()
self.master.geometry('550x550')
self.label_5 = tk.Checkbutton(self.master, text="I agree to the", bg='white', width=14, font=("Arial", 8),
command=self.activator)
self.label_5.place(x=112, y=410)
self.button_2 = tk.Button(text='Proceed', width=20, bg='white', state=tk.DISABLED, bd=1,
highlightbackground='black', font=("Arial", 10))
self.button_2.place(x=208, y=512)
self.master.mainloop()
def activator(self):
if self.button_2['state'] == tk.DISABLED:
self.button_2['state'] = tk.NORMAL
else:
self.button_2['state'] = tk.DISABLED
if __name__ == '__main__':
Test()
推荐阅读
- c - C中的递归问题
- java - java http请求必需的字符串参数'用户名'不存在
- node.js - AWS Lambda 使用异步代码返回 NULL
- java - 使用 if 条件与三元运算符循环,后者更快,为什么
- node.js - 在nodejs中创建原子函数
- html - QWebengineView:用id打开本地文件
- visual-studio-code - 如何区分输出面板和其他 activeTextEditor?
- android - Android应用程序中特定帧内的自动拍照功能
- typescript - 这在expressJS中可能吗?
- c# - UWP - 如何通过右键单击或右键点击获取 ListViewItem 值而不选择无 Listviewitem?