如何回调kivy中的复选框?
How to call back checkboxs in kivy?
self.t_and_c = Label(text='Accept terms and conditions: ')
self.inside.add_widget(self.t_and_c)
self.checkbox_first = CheckBox(active=False)
self.checkbox_first.bind(active = self.checkbox_status)
self.inside.add_widget(self.checkbox_first)
self.pvt_policy = Label(text='Accept private policy: ')
self.inside.add_widget(self.pvt_policy)
self.checkbox_second = CheckBox(active=False)
self.checkbox_second.bind(active = self.checkbox_status)
self.inside.add_widget(self.checkbox_second)
self.inside.add_widget(Label())
self.enter = Button(text='Enter information', disabled=True)
self.inside.add_widget(self.enter)
def checkbox_status(self, instance):
if self.checkbox_first.active == True and self.checkbox_second.active == True:
self.enter.disabled == False
else:
self.enter.disabled == True
我想创建一个函数来禁用一个按钮,直到两个复选框被选中 'checked'。我正在尝试调用复选框,但出现此错误:
TypeError: checkbox_status() takes 2 positional arguments but 3 were given
我很困惑为什么我需要另一个输入参数,我们将不胜感激,但请用 Python 语言而不是 kv
语言给出答案?
绑定到 Checkbox
的 active
属性 的回调期望使用两个参数调用该回调,即 CheckBox
实例和active
属性。由于您在 checkbox_status()
中指定的方法:
self.checkbox_second.bind(active = self.checkbox_status)
是 class 的一个实例方法(注意 self.
)一个 self
参数被自动插入作为方法调用的第一个参数。这导致 checkbox_status()
方法的参数列表为:
checkbox_status(self, checkbox_instance, new_value)
所以方法的签名应该是:
def checkbox_status(self, instance, new_value):
或
def checkbox_status(self, instance, *args):
self.t_and_c = Label(text='Accept terms and conditions: ')
self.inside.add_widget(self.t_and_c)
self.checkbox_first = CheckBox(active=False)
self.checkbox_first.bind(active = self.checkbox_status)
self.inside.add_widget(self.checkbox_first)
self.pvt_policy = Label(text='Accept private policy: ')
self.inside.add_widget(self.pvt_policy)
self.checkbox_second = CheckBox(active=False)
self.checkbox_second.bind(active = self.checkbox_status)
self.inside.add_widget(self.checkbox_second)
self.inside.add_widget(Label())
self.enter = Button(text='Enter information', disabled=True)
self.inside.add_widget(self.enter)
def checkbox_status(self, instance):
if self.checkbox_first.active == True and self.checkbox_second.active == True:
self.enter.disabled == False
else:
self.enter.disabled == True
我想创建一个函数来禁用一个按钮,直到两个复选框被选中 'checked'。我正在尝试调用复选框,但出现此错误:
TypeError: checkbox_status() takes 2 positional arguments but 3 were given
我很困惑为什么我需要另一个输入参数,我们将不胜感激,但请用 Python 语言而不是 kv
语言给出答案?
绑定到 Checkbox
的 active
属性 的回调期望使用两个参数调用该回调,即 CheckBox
实例和active
属性。由于您在 checkbox_status()
中指定的方法:
self.checkbox_second.bind(active = self.checkbox_status)
是 class 的一个实例方法(注意 self.
)一个 self
参数被自动插入作为方法调用的第一个参数。这导致 checkbox_status()
方法的参数列表为:
checkbox_status(self, checkbox_instance, new_value)
所以方法的签名应该是:
def checkbox_status(self, instance, new_value):
或
def checkbox_status(self, instance, *args):