【发布时间】:2018-08-18 05:21:55
【问题描述】:
对于我的 kivy 项目,我正在尝试在其中创建一个带有 BooleanProperties 的字典。我想将其用于 6 个按钮,我希望启用和禁用一定数量的按钮。我已经尝试过按照我通常的方式去做(为了清楚起见,只使用 3 个属性):
class MainScreen(ScreenManager):
disable_1 = BooleanProperty(True)
disable_2 = BooleanProperty(True)
disable_3 = BooleanProperty(True)
buttonstates = []
我的按钮 .kv 代码如下所示:
<MainScreen>:
BoxLayout:
id: box_buttons_1
orientation: 'vertical'
Button:
background_color: (1.0, 0.0, 0.0, 1.0)
text: "place 1"
on_press: show_pictures_manager.get_children_of_screenmanager(1)
disabled: root.disable_1
Button:
background_color: (0.8, 0.8, 0.0, 1.0)
text: "place 1.1"
disabled: root.disable_2
Button:
background_color: (1.0, 0.0, 1.0, 1.0)
text: "place 1.2"
disabled: root.disable_3
我想用来遍历它们并更改它们的值的函数是:
def change_button_states(self, amount_of_buttons_to_change):
self.buttonstates = [self.disable_1, self.disable_2, self.disable_3]
for no in range(3):
self.buttonstates[1] = False
print(self.buttonstates)
此代码打印 3 次:
[True, False, True]
很好,但是(在这种情况下)第二个按钮没有启用自身。我做错了什么?
编辑:
我注意到我创建了一个按钮状态实例,但没有在我的函数中与之通信。我将其更改为与 self.buttonstates 实例进行通信,但它并没有改变任何事情。
【问题讨论】:
-
什么是
amount_of_buttons_to_change? -
我正在构建的项目是关于检测图片中的项目。所以我想检测图片中的项目,并根据应用程序找到多少项目激活按钮。最后,这将意味着按钮响应它们所代表的项目。编辑:我意识到您可能会问,因为我似乎没有使用该变量。我希望在 range() 函数中使用它:
for no in range(amount_of_buttons_to_change):