首页 > 解决方案 > 如何使用显示复选框代表什么的标签将 kivymd MDCheckBox 添加到 kv RecycleView?

问题描述

我目前正在开发一个应用程序,您可以在其中存储您的计划并使用Python Kivy 和 KivyMD获取通知,但我遇到了MDCheckbox问题。所有当前计划都将显示在菜单窗口上,格式如下:PlanText (由 MDLabel 表示)IsDone (由 MDCheckbox 表示)。但是,当应用程序运行时,除了复选框之外什么都没有 -没有任何计划文本正在运行的应用程序的屏幕截图

我的一些 Python 代码:

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 []

我的一些 KV 代码:

<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

提前致谢!

标签: pythonkivykivy-language

解决方案


我怀疑您需要向您的班级添加一个text属性,如下所示:MyButton

<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

您可能需要对MDCheckBox.


推荐阅读