Python 3.9.0
目标
一种查找当前是否选择了 MDChip 的方法
问题
使用check
MDChips 的属性来确定是否选择了芯片返回True,即使该芯片被选择与否。我希望它在未选择芯片时返回false 。
示例代码
from kivy.lang import Builder
from kivymd.uix.screen import Screen
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.chip import MDChip
KV = '''
MDRaisedButton:
id: button
text: "PRESS ME"
pos_hint: {"center_x": .5, "center_y": .5}
on_release: app.testing()
'''
class Test(MDApp):
def build(self):
self.screen = Screen()
btn = Builder.load_string(KV)
self.screen.add_widget(btn)
return self.screen
def testing(self):
objects = {0.2 : "Item 1", 0.5 : "Item 2", 0.8 : "Item 3"}
for pos, item in objects.items():
chip = MDChip(
text = item,
check = True,
pos_hint = {"center_x":pos, "center_y":0.4},
on_release = self.fetch
)
self.screen.add_widget(chip)
def fetch(self, chip_instance):
if chip_instance.check == True:
print("True")
else:
print("False")
Test().run()
KV 脚本在上面提供的 main.py 文件中