Search code examples
pythonkivykivy-language

How to add kivymd MDCheckBox to kv RecycleView with a Label that shows what the checkbox stands for?


I'm currently working on an application where you can store your plans and get notifications using Python Kivy and KivyMD, but I have a problem with the MDCheckbox. All current plans are meant to be displayed on the Menu Window, in this format: PlanText (represented by an MDLabel) and IsDone (represented by an MDCheckbox). However, when the app is running, there is nothing, but the checkboxes - no plan texts whatsoever: Screenshot of the running app

A bit of my Python code:

class PlansToDisplay(RecycleView):
    """This class is used to display user's current plans"""

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @property
    def data(self):
        # If a user has already planned something, this function returns a list
        # of dicts containing plan texts. If not, it just returns an empty list.
        
        # -> For the record, -plan- is a tuple represented in
        # the following format: (PlanText, PlanPriority, IsDone)

        if current_day in STORAGE.to_dict.keys():
            return [{"text": str(plan[0])} for plan in STORAGE.to_dict[current_day]]
        return []

A bit of my KV code:

<MyButton@GridLayout>:
    cols: 2
    
    # Is supposed to contain a plan text
    MDLabel:
        size_hint: .8, None
    
    # Is meant for keeping track of whether the 
    # user's finished the plan or not.
    MDCheckbox:
        size_hint: .2, None


<PlansToDisplay>:
    viewclass: "MyButton"
    grid: grid

    RecycleGridLayout:
        id: grid
        orientation: 'vertical'

        cols: 1
        size_hint: 1, None
        default_size: None, None
        default_size_hint: 1, None
        height: self.minimum_height

Thanks in advance!


Solution

  • I suspect you need to add a text property to your MyButton class, something like this:

    <MyButton@GridLayout>:
        cols: 2
        text: ''
        
        # Is supposed to contain a plan text
        MDLabel:
            size_hint: .8, None
            text: root.text
        
        # Is meant for keeping track of whether the 
        # user's finished the plan or not.
        MDCheckbox:
            size_hint: .2, None
    

    You will likely need to do something similar for the MDCheckBox.