简体   繁体   English

Kivy 带有复选框的 MDList。 选择具有活动复选框的正确项目

[英]Kivy MDList with checkboxes. Choose right item with active checbox

I use this method https://kivymd.readthedocs.io/en/latest/components/list/index.html#custom-list-item from the documentation to create a list with checkboxes.我使用文档中的这种方法https://kivymd.readthedocs.io/en/latest/components/list/index.html#custom-list-item创建带有复选框的列表。 I use the checkbox with the group option.我使用带有 group 选项的复选框。 And everything works great.一切都很好。 But I can't figure out how to access the right checkbox?但我不知道如何访问正确的复选框? How do I understand which element of the list contains the active checkbox?我如何理解列表的哪个元素包含活动复选框?

My PY file我的 PY 文件

class ListItemWithCheckbox(OneLineAvatarIconListItem):
    '''Custom list item.'''
    icon = StringProperty("android")


class RightCheckbox(IRightBodyTouch, MDCheckbox):
    '''Custom right container.'''
    def on_checkbox_active(self, checkbox, value):
        if value:
            print('The checkbox', checkbox, 'is active', 'and', checkbox.state, 'state')
            print(value)
        else:
            print('The checkbox', checkbox, 'is inactive', 'and', checkbox.state, 'state')
            print(value)

class DatabaseListScreen(Screen):
    store = JsonStore('Config/database_settings.json')
    def on_enter(self, *args):
        icons = list(md_icons.keys())
        with open('Config/Database_settings.json', 'r', encoding='utf-8') as fh:
            data = json.load(fh)
        for i in range(3):
            self.ids.scroll.clear_widgets()
        for i in range(3):
            config_name = data["Database"][i]["config"]
            listitem = ListItemWithCheckbox(text=config_name, icon=icons[i])
            self.ids.scroll.add_widget(listitem)




    def build(self):
        return Builder.load_file("Enter/DatabaseListScreen.kv")

KV:千伏:

<ListItemWithCheckbox>:

    IconLeftWidget:
        icon: root.icon

    RightCheckbox:
        group: 'group'
        on_active: self.on_checkbox_active(*args)
<DatabaseListScreen>:
    name: "database_list_screen"
    BoxLayout:
        orientation: "vertical"

        ScrollView:

            MDList:
                id: scroll
    MDRaisedButton:
        text: 'Назад'
        multiline: True
        halign: "center"
        pos_hint: {"center_x": .5, "center_y": .4}
        on_press: app.root.current = 'database_settings_screen'
<ListItemWithCheckbox>

    [...]

    RightCheckbox:
        id: check
        [...]


def get_active_check(self):
    for ListItemWithCheckbox in self.DatabaseListScreen.ids.scroll.children:
        if ListItemWithCheckbox.ids.check.active:
            print(ListItemWithCheckbox)
            break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kivy:在 MDList 中填充 TextFieldMD - Kivy: Padding TextFieldMD in MDList 通过单击 MDList 的项目切换到其他屏幕 - Switching to other screen through clicking an item of an MDList 更改为包含 MDDataTable --kivy 的屏幕后,MDList 的按钮卡住 - The buttons of MDList gets stuck after changing to Screen contaning MDDataTable --kivy 仅与复选框的选择相关的不同功能列表相交。 错误:对于集合对象使用 bool - Intersect lists of different functions only in relation to the selection of the checkboxes. Error: with bool for set objects 在 Kivy 中创建没有 id 的动态列表时,从 MDList 中删除几个 TwoLineListItem - Delete several TwoLineListItem from an MDList when creating a dinamic list without an ids in Kivy 第一个 Python/Kivy/KivyMD 应用程序。 无法将文本字段中的数据保存到变量 + 数据绑定到 MDList - First Python/Kivy/KivyMD App. Unable to save data from textfield to variable + data binding to MDList Kivy Switch on_active - Kivy Switch on_active Kivy右键菜单 - Kivy right click menu Kivy Python右击 - Kivy Python Right Click 如何从数组中随机选择 N 个项目并将它们与另一个数组中的正确项目相关联? - How to randomly choose N items from array and associate them with the right item from another array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM