0

尝试运行我的代码时,我不断收到错误消息。我不觉得我真正理解动态类是如何工作的。

我当前得到的错误是“无效的类名”

<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"
                                

如何正确设置新的动态类?我想创建多个新类。我是否必须在我的 kivy 文件的开头编写这些类,或者我可以将它们放在我的 kv 文件中的任何位置?

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

4

1 回答 1

0

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

你的语法也是错误的,当你pagesButton在另一个规则中使用你时,你应该省略它<>周围的。

我当前得到的错误是“无效的类名”

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

于 2020-08-05T22:13:29.173 回答