MDList with checkbox set active

545 views
Skip to first unread message

Gibbsy FPV

unread,
May 26, 2022, 6:13:30 AM5/26/22
to Kivy users support
Hi All

Can some help i have a custom MDList with checkbox Is there a way I can check a box within python code. I can print out what checkbox is selected etc.. but im looking for a way to set/change the default active.

Any help will be much appreciated.

Thanks
GibbsFPV

Elliot Garbus

unread,
May 26, 2022, 12:29:10 PM5/26/22
to kivy-...@googlegroups.com

Here is an example:

 

from kivy.lang import Builder
from kivy.properties import StringProperty

from kivymd.app import MDApp
from kivymd.uix.list import IRightBodyTouch, OneLineAvatarIconListItem
from kivymd.uix.selectioncontrol import MDCheckbox
from kivymd.icon_definitions import md_icons


KV =
'''
<ListItemWithCheckbox>:
    IconLeftWidget:
        icon: root.icon
    RightCheckbox:
        id: cb          # crate an id to address the checkbox

MDBoxLayout:
    orientation: 'vertical'
    ScrollView:
        MDList:
            id: scroll
    MDBoxLayout:
        size_hint_y: None
        height: dp(48)
        Button:
            text: 'Set CheckBoxes'
            on_release: app.set_checkboxes(True)
        Button:
            text: 'Clear CheckBoxes'
            on_release: app.set_checkboxes(False)
        Button:
            text: 'Set Item 5'
            on_release: app.set_one_checkbox('Item 5', True)
'''


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


class RightCheckbox(IRightBodyTouch, MDCheckbox):
   
'''Custom right container.'''
   
pass


class
MainApp(MDApp):
   
def build(self):
       
return Builder.load_string(KV)

   
def on_start(self):
        icons =
list(md_icons.keys())
       
for i in range(30):
           
self.root.ids.scroll.add_widget(
                ListItemWithCheckbox(
text=f"Item {i}", icon=icons[i])
            )

   
def set_checkboxes(self, v):  # pass True to set, false to clear
        # the children of scroll are the ListItemWithCheckBoxWidgets
       
for w in self.root.ids.scroll.children:
            w.ids.cb.active = v

   
def set_one_checkbox(self, s, v):
       
# pass a string to identify a checkbox and value to set
       
for w in self.root.ids.scroll.children:
           
if w.text == s:
                w.ids.cb.active = v
               
break




MainApp().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/c913eff8-9082-42b2-ba2d-49500ef374fdn%40googlegroups.com.

 

Gibbsy FPV

unread,
May 27, 2022, 9:51:06 AM5/27/22
to Kivy users support
Hi Elliot

You are so kind thank you.

Thanks
GibbsyFPV

Reply all
Reply to author
Forward
0 new messages