导航抽屉中 kivy 中的类型错误

Posted

技术标签:

【中文标题】导航抽屉中 kivy 中的类型错误【英文标题】:A Type error in kivy in Navigation drawer 【发布时间】:2021-06-07 09:09:19 【问题描述】:

我在运行以下程序时遇到了 kivy 错误

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager,Screen

class mainwin(ScreenManager):
    pass
class AppScreenOne(Screen):
    pass
class User1(Screen):
    pass
class User2(Screen):
    pass
KV = '''


mainwin:
    AppScreenOne:
    User1:
    User2:

<AppScreenOne>:
    name:'main'
 
    MDFloatingActionButton:
        icon: "android"
        md_bg_color: app.theme_cls.primary_color
    ScrollView:
    
        MDList:

            OneLineAvatarIconListItem:
                text: "User1"
                on_release:app.root.current="userone"
    
                IconLeftWidget:
                    icon: "github"

            OneLineAvatarIconListItem:
                text: "User2"
                on_release:app.root.current="usertwo"

                IconLeftWidget:
                    icon: "gitlab"

<User1>:
    name:"userone"
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'bottom'
        MDTextField:
            hint_text: "Message"
            mode: "fill"
            fill_color: 0, 0, 0,.4
            max_text_length: 5
            size_hint: (0.7, 0.1)
    AnchorLayout:
        anchor_x: 'right'
        anchor_y: 'bottom'
        MDRaisedButton:
            text: "Send"
            md_bg_color: 0, 0, 0, 1
            size_hint:(0.3, 0.1)
<User2>:
    name:"usertwo"
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'bottom'
        MDTextField:
            hint_text: "Message"
            mode: "fill"
            fill_color: 0, 0, 0,.4
            max_text_length: 5
            size_hint: (0.7, 0.1)
    AnchorLayout:
        anchor_x: 'right'
        anchor_y: 'bottom'
        MDRaisedButton:
            text: "Send"
            md_bg_color: 0, 0, 0, 1
            size_hint:(0.3, 0.1)
    MDNavigationDrawer:
        ScrollView:    
                MDList:    
                    OneLineListItem:
                        text: "Chats"
                        on_press:
                            root.set_state("close")
                            root.mainwin.current = "main"
'''


class MainApp(MDApp):
    def build(self):
        return Builder.load_string(KV)

class ContentNavigationDrawer(BoxLayout):
    screen_manager = ObjectProperty()
    nav_drawer = ObjectProperty()
MainApp().run()

这是错误。 它仅在我尝试添加 MD 导航抽屉后才开始。

C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\Scripts\python.exe C:/Users/susheel/PycharmProjects/learningkivyfromedureka/venv/Scripts/listig.py
[INFO   ] [Logger      ] Record log in C:\Users\susheel\.kivy\logs\kivy_21-03-09_25.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.3.1
[INFO   ] [Kivy        ] v2.0.0
[INFO   ] [Kivy        ] Installed at "C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\Scripts\python.exe"
[INFO   ] [Factory     ] 186 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [KivyMD      ] 0.104.2.dev0, git-Unknown, 2021-03-08 (installed at "C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\lib\site-packages\kivymd\__init__.py")
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.4.0 - Build 21.20.16.4627'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel(R) HD Graphics 520'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 4
[INFO   ] [GL          ] Shading version <b'4.40 - Build 21.20.16.4627'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [GL          ] NPOT texture support is available
 Traceback (most recent call last):
   File "C:/Users/susheel/PycharmProjects/learningkivyfromedureka/venv/Scripts/listig.py", line 101, in <module>
     MainApp().run()
   File "C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\lib\site-packages\kivy\app.py", line 949, in run
     self._run_prepare()
   File "C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\lib\site-packages\kivy\app.py", line 919, in _run_prepare
     root = self.build()
   File "C:/Users/susheel/PycharmProjects/learningkivyfromedureka/venv/Scripts/listig.py", line 96, in build
     return Builder.load_string(KV)
   File "C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\lib\site-packages\kivy\lang\builder.py", line 373, in load_string
     parser = Parser(content=string, filename=fn)
   File "C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\lib\site-packages\kivy\lang\parser.py", line 402, in __init__
     self.parse(content)
   File "C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\lib\site-packages\kivy\lang\parser.py", line 511, in parse
     objects, remaining_lines = self.parse_level(0, lines)
   File "C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\lib\site-packages\kivy\lang\parser.py", line 614, in parse_level
     _objects, _lines = self.parse_level(
   File "C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\lib\site-packages\kivy\lang\parser.py", line 614, in parse_level
     _objects, _lines = self.parse_level(
   File "C:\Users\susheel\PycharmProjects\learningkivyfromedureka\venv\lib\site-packages\kivy\lang\parser.py", line 674, in parse_level
     if current_property[:3] == 'on_':
 TypeError: 'NoneType' object is not subscriptable

Process finished with exit code 1

【问题讨论】:

那么请回答 看起来kvMDListMDNavigationDrawer 中的缩进不正确。 【参考方案1】:

缩进不正确。 MDList 项目缩进太远。这是更正的部分:

MDNavigationDrawer:
    ScrollView:    
        MDList:    
            OneLineListItem:
                text: "Chats"
                on_press:
                    root.set_state("close")
                    root.mainwin.current = "main"

【讨论】:

以上是关于导航抽屉中 kivy 中的类型错误的主要内容,如果未能解决你的问题,请参考以下文章

kivymd 中的 AssertionError

导航抽屉内的 ParseQuery 错误验证程序拒绝了类 com.parse.ParseQuery

在片段导航抽屉中。我在 getActionBar 中遇到错误。我能知道错误的原因和错误吗

KIVY 的 super() 类中的类型错误

Android Studio 中导航抽屉预览中的渲染问题

带有导航抽屉的片段中的 ListView 和 ViewPager