Kivymd错误self._shadow = App.get_running_app()。theme_cls.quad_shadow AttributeError:'NoneType'对象没有属性'theme_cls'

时间:2019-12-14 13:32:22

标签: python kivy kivy-language

我正在使用kivy和kivymd。我试图在屏幕内创建MDToolbar。当我运行时,出现上述错误。希望你能帮助我。这是我的main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import Image
from kivymd.uix.navigationdrawer import MDNavigationDrawer
from kivymd.theming import ThemeManager
from kivy.properties import ObjectProperty
from kivymd.uix.toolbar import MDToolbar

class HomeScreen(Screen):
    pass

class ImageButton(ButtonBehavior, Image):
    pass

class SettingsScreen(Screen):
    pass

GUI = Builder.load_file("main.kv")


class MainApp(App):
    theme_cls = ThemeManager()
    def build(self):
        return GUI

    def change_screen(self, screen_name):
        #Get the screen manager from the kv file
        screen_manager = self.root.ids['screen_manager']
        screen_manager.current = screen_name


MainApp().run()

及其main.kv文件

#:include kv/homescreen.kv
#:include kv/settingsscreen.kv

GridLayout:
    cols: 1
    ScreenManager:
        id: screen_manager
        HomeScreen:
            id: home_screen
            name: "home_screen"

        SettingsScreen:
            name: "settings_screen"
            id: settings_screen

我的homescreen.kv写为:

#:import MDToolbar kivymd.uix.toolbar.MDToolbar
#:import utils kivy.utils

<HomeScreen>:
    NavigationLayout:
        id: nav_layout
        MDNavigationDrawer:
            id: nav_drawer
            name: 'nav_drawer'
            #drawer_logo: 'user.png'
            NavigationDrawerIconButton:
                icon: 'face'
                text: 'text'
            NavigationDrawerIconButton:
                icon: 'email'
                text: 'text'

            NavigationDrawerIconButton:
                icon: 'phone'
                text: 'number'
            NavigationDrawerIconButton:
                icon: 'cake'
                text: 'date'
            NavigationDrawerIconButton:
                icon: 'city'
                text: 'city'
            NavigationDrawerIconButton:
                icon: 'settings'
                text: 'Settings'

        BoxLayout:
            orientation: 'vertical'
            canvas:
                Color:
                    rgb: utils.get_color_from_hex("#3983F2")
                Rectangle:
                    size: self.size
                    pos: self.pos
            MDToolbar:
                title: "Toolbar"

我跑步时遇到的错误是

     Traceback (most recent call last):
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 696, in _apply_rule
     setattr(widget_set, key, value)
   File "kivy/weakproxy.pyx", line 35, in kivy.weakproxy.WeakProxy.__setattr__
   File "kivy/properties.pyx", line 497, in kivy.properties.Property.__set__
   File "kivy/properties.pyx", line 544, in kivy.properties.Property.set
   File "kivy/properties.pyx", line 599, in kivy.properties.Property.dispatch
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1120, in kivy._event.EventObservers._dispatch
   File "kivy/properties.pyx", line 1318, in kivy.properties.ReferenceListProperty.trigger_change
   File "kivy/properties.pyx", line 1333, in kivy.properties.ReferenceListProperty.trigger_change
   File "kivy/properties.pyx", line 599, in kivy.properties.Property.dispatch
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1120, in kivy._event.EventObservers._dispatch
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivymd/uix/elevation.py", line 111, in _update_shadow
     self._shadow = App.get_running_app().theme_cls.quad_shadow
 AttributeError: 'NoneType' object has no attribute 'theme_cls'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "/home/pakomoaro/PycharmProjects/fypp/main.py", line 20, in <module>
     GUI = Builder.load_file("main.kv")
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 301, in load_file
     return self.load_string(data, **kwargs)
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 405, in load_string
     rule_children=rule_children)
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 659, in _apply_rule
     child, crule, rootrule, rule_children=rule_children)
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 657, in _apply_rule
     root=rctx['ids']['root'], rule_children=rule_children)
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/uix/widget.py", line 469, in apply_class_lang_rules
     rule_children=rule_children)
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 538, in apply
     rule_children=rule_children)
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 659, in _apply_rule
     child, crule, rootrule, rule_children=rule_children)
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 659, in _apply_rule
     child, crule, rootrule, rule_children=rule_children)
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 657, in _apply_rule
     root=rctx['ids']['root'], rule_children=rule_children)
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/uix/widget.py", line 469, in apply_class_lang_rules
     rule_children=rule_children)
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 538, in apply
     rule_children=rule_children)
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 707, in _apply_rule
     e), cause=tb)
 kivy.lang.builder.BuilderException: Parser: File "<inline>", line 22:
 ...
      20:<MDToolbar>
      21:    size_hint_y: None
 >>   22:    height: root.theme_cls.standard_increment
      23:    padding: [root.theme_cls.horizontal_margins - dp(12), 0]
      24:    opposite_colors: True
 ...
 AttributeError: 'NoneType' object has no attribute 'theme_cls'
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 696, in _apply_rule
     setattr(widget_set, key, value)
   File "kivy/weakproxy.pyx", line 35, in kivy.weakproxy.WeakProxy.__setattr__
   File "kivy/properties.pyx", line 497, in kivy.properties.Property.__set__
   File "kivy/properties.pyx", line 544, in kivy.properties.Property.set
   File "kivy/properties.pyx", line 599, in kivy.properties.Property.dispatch
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1120, in kivy._event.EventObservers._dispatch
   File "kivy/properties.pyx", line 1318, in kivy.properties.ReferenceListProperty.trigger_change
   File "kivy/properties.pyx", line 1333, in kivy.properties.ReferenceListProperty.trigger_change
   File "kivy/properties.pyx", line 599, in kivy.properties.Property.dispatch
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1120, in kivy._event.EventObservers._dispatch
   File "/home/pakomoaro/PycharmProjects/FYP/venv/lib/python3.6/site-packages/kivymd/uix/elevation.py", line 111, in _update_shadow
     self._shadow = App.get_running_app().theme_cls.quad_shadow

1 个答案:

答案 0 :(得分:1)

如果您使用的是当前版本的KivyMD(0.102.1),请尝试将from kivy.app import App替换为from kivymd.app import MDApp,然后执行以下操作:

class MainApp(App):
    theme_cls = ThemeManager()
    def build(self):
        return GUI

与此:

class MainApp(MDApp):
    def build(self):
        return GUI

还必须在root widget中更改main.kv
像这样将kivy小部件用作root是不明智的。

您必须首先在.py中创建一个类:

class BaseWidget(GridLayout):
    pass

并更改您的.kv

<BaseWidget>:
    cols: 1
    ScreenManager:
        id: screen_manager
        HomeScreen:
            id: home_screen
            name: "home_screen"

        SettingsScreen:
            name: "settings_screen"
            id: settings_screen

此后,您必须使用build方法返回您的根窗口小部件:

class MainApp(MDApp):

    def build(self):
        return BaseWidget()