在kivy中设置动态类

Set up dynamic classes in kivy

我在尝试 运行 我的代码时总是遇到错误。我不觉得我真正理解动态 class 是如何工作的。

我目前得到的错误是'Invalid class name'

<pagesButton@MDRectangleFlatButton>
    canvas:
        Color:
            rgba: 0, 0, 0, 1
        Line:
            width: 4
            ellipse: (self.x, self.y, 120, 120)
    Image:
        size_hint: None, None
        size: 60, 60
        keep_ratio: False
        allow_stretch: True




NavigationLayout:

    ScreenManager:
        id: screen_manager

        Screen:
            name: "screen1"

            GridLayout:
                cols: 1
                rows: 6

                canvas.before:
                    Rectangle:
                        size: self.size
                        pos: self.pos
                        source: 'images/MainGradient.png'



                MDToolbar:
                    id: FSZ_toolbar
                    left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
                    #icon_color: 0,0,0,1
                    theme_text_color: 'Custom'
                    md_bg_color: 1,1,1,1
                    elevation: 0

                    BoxLayout:
                        size_hint_x: None
                        width: 1000

                        Image:
                            source: 'images/Logo1_textHeader.png'
                            size_hint_x: None
                            width: 500
                            allow_stretch: True
                            keep_ratio: True
                            pos_hint_x: {'left': .5}



                GridLayout:  #Different pages buttons
                    cols: 10
                    rows: 1
                    size_hint_y:  None
                    height: 150
                    spacing: 60
                    padding: 20, 60, 20, 0


                    MDRectangleFlatButton:
                        canvas:
                            Color:
                                rgba: 0, 0, 0, 1
                            Line:
                                width: 4
                                ellipse: (self.x, self.y, 120, 120)

                        BoxLayout:
                            size_hint: None, None
                            #pos_hint: {'center': .5, 'top': .5}
                            width: 30
                            height: 25

                            Image:
                                source: "Nav_icons/Events.png"
                                size_hint: None, None
                                size: 60, 60
                                keep_ratio: False
                                allow_stretch: True
                                #pos_hint: {'left': .5, 'top': 1.1}


                    <pagesButton>:
                         Image:
                             source: "Nav_icons/Radio.png"
                                

如何正确设置新动态 class?我想创建多个新的 classes。我是否必须将这些 classes 全部写在我的 kivy 文件的开头,或者我可以将它们放在我的 kv 文件的任何地方吗?

我是否还必须在我的 py 文件中编写代码才能使其工作?或者我可以只从我的 kivy 文件让它全部工作吗?如果我的 py 文件中需要代码,我应该写什么?

确保您的 class 名称以大写字母开头。 kv 使用这个事实来区分小部件和属性。

此外,您的语法错误,在另一条规则中使用 pagesButton 时,您应该省略它周围的 <>

The error im currently getting is 'Invalid class name'

上面的细节已经解释过了,但是一般点不要只写出错误的一小部分,总是post完整的东西。它通常会提供更多上下文,对任何试图帮助您的人都有用。