jeudi 20 juin 2019

Python KivyMD: How is it possible to use on_active on MDCheckboxes?

I'm currently facing some problems with the MDCheckbox. When I used the default Kivy Checkbox, the on_active parameter in the kv code seemed to work pretty good. But now I am trying to use the KivyMD MDCheckbox module with an MDList and try to add a function to the Checkbox via the on_active parameter:

Part of the kv code

#:kivy 1.11.0
#:import MDCard kivymd.card.MDCard
#:import MDCheckbox kivymd.selectioncontrols.MDCheckbox
#:import MDList kivymd.list.MDList
#:import OneLineAvatarIconListItem kivymd.list.OneLineAvatarIconListItem

<ListItemWithCheckbox@OneLineAvatarIconListItem>:
    MyAvatar:
        source: 'src/hdl.jpg'
    MyCheckbox:

<LayoutPy>
    orientation: 'vertical'

    FloatLayout:

        MDCard:
            size_hint: .8, .5
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}

            BoxLayout:
                orientation: 'horizontal'
                spacing: 20
                name: 'lists'

                ScrollView:

                    MDList:
                        id: scroll

                        ListItemWithCheckbox:
                            id: ckbx1
                            text: 'Box 1'
                            active: False
                            on_active: root.printS('Text 1')


                        ListItemWithCheckbox:
                            id: ckbx2
                            text: 'Box 2'
                            active: False
                            on_active: root.printS('Text 2')

                        ListItemWithCheckbox:
                            id: ckbx3
                            text: 'Box 3'
                            active: False
                            on_active: root.printS('Text 3')

Part of the Python code

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivymd.theming import ThemeManager
from kivy.uix.image import Image
from kivymd.list import IRightBodyTouch, ILeftBody
from kivymd.selectioncontrols import MDCheckbox
from kivy.lang import Builder

class LayoutPy(FloatLayout):
    def __init__(self, **kwargs):
        super(LayoutPy, self).__init__(**kwargs)

    def printS(self, text):
        print(text)

class MyCheckbox(IRightBodyTouch, MDCheckbox):
    pass


class MyAvatar(ILeftBody, Image):
    pass

Builder.load_file(design.kv)

class KivyGUI(App):
    theme_cls = ThemeManager()
    theme_cls.primary_palette = ("Teal")
    title = ("App")

    def build(self):
        c = LayoutPy()
        return c

if __name__ == "__main__":
    KivyGUI().run()

I think unlike normal Kivy, KivyMD needs an additional active parameter to add functions to an MDCheckbox. I've tried to set an active parameter with a boolean value of True/False (I tried both and nothing seemed to work). I changed some parts of the code to make it easier for you but if you want to take a look on the original source code you can see it here. (In the original source code the MDCheckboxes are on line 143.

Many thanks in advance!




Aucun commentaire:

Enregistrer un commentaire