首页 > 解决方案 > 使用 kivy 使用另一个复选框选中或取消选中复选框

问题描述

我有 2 个复选框,我需要它们按如下方式工作:

如果第一个未被选中(启用项目参与者)并且第二个已被选中(仅允许使用..),它也将被取消选中。但是当我检查第一个时,第二个将保持未选中状态。如果未选中第二个复选框,则only_def_actors需要设置一个布尔变量。False

我已经尝试过了,但是我得到了 `TypeError: 'bool' object is not callable。我不知道应该更改哪个属性才能使其正常工作。或者也许它更容易做到?

在 main.kv 中:

#defined id of checkbox to use in .py
only_def_act: only_def_actors


Label:
    text: "Enable project actors"
CheckBox:
    on_active: root.actors_checkbox_click(self, self.active)
    active: True

Label:
    text: "Allow using only defined actors"
CheckBox:
    id: only_def_actors
    on_active: root.only_def_actors_checkbox_click(self, self.active)

在 main.py 中:

#defined variables
only_def_act = ObjectProperty(None)
actors_status = BooleanProperty(True) #this one is set to be checked by default
only_def_actors = BooleanProperty(False) #this one is set to be unchecked by default

# Callback for the checkbox
def only_def_actors_checkbox_click(self, instance, value):
    if value is True:
        self.only_def_actors = True
        print("T")
    else:
        self.only_def_actors = False
        self.only_def_act.active(False)
        print("F")

# Callback for the checkbox
def actors_checkbox_click(self, instance, value):
    if value is True:
        self.actors_status = True
        print("T")
    else:
        self.actors_status = False
        self.only_def_actors_checkbox_click(instance, False)
        print("F")

标签: pythoncheckboxkivy

解决方案


您从这一行得到错误:

self.only_def_act.active(False)

它试图调用CheckBox active属性(它是 a BooleanProperty),就好像它是一个方法一样。

也许你的意思是:

self.only_def_act.active = False

推荐阅读