简体   繁体   中英

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. I use the checkbox with the group option. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM