KivyMD AttributeError:“ NoneType”对象没有属性“ theme_cls”

时间:2019-12-23 22:12:48

标签: python-3.x kivy kivy-language

我在使KivyMD正常工作时遇到麻烦,我在这里阅读了很多有关此错误的问题,但没有一个起作用。下面是一个最小的可运行示例,我知道问题出在MyApp类中的代码上,但我不知道为什么它不起作用。预先感谢您的帮助

main.py

import kivy
from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty, NumericProperty, ListProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivymd.theming import ThemeManager
import mysql.connector


Window.clearcolor = (1,1,1,1)


class Information(Screen):
    pass


class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("kivy.kv")
sm = WindowManager()

screens = [Information(name="information")]
for screen in screens:
    sm.add_widget(screen)

sm.current = "information"


class MyApp(App):
    theme_cls = ThemeManager()

    def build(self):
        return sm


if __name__ == '__main__':
    MyApp().run()

kivy.kv

<Information>:
    name: "information"

    FloatLayout:

        ActionBar:
            pos_hint: {'top': 1}
            ActionView:
                background_image: ""
                background_color: 0.2,0.6,1,1
                ActionPrevious:
                    app_icon: "white menu.png"
                    with_previous: False
                ActionOverflow:
                ActionButton:
                    app_icon: "close.png"
                    on_release: print("Button pressed")

        MDRaisedButton:
            text: "Test"

完全错误

 AttributeError: 'NoneType' object has no attribute 'theme_cls'
   File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\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 "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivymd\uix\elevation.py", line 126, in _update_shadow
     self._shadow = App.get_running_app().theme_cls.quad_shadow

1 个答案:

答案 0 :(得分:1)

您只需在定义Builder.load_file()类之后执行MyApp。您可以在build()方法中移动它:

class MyApp(App):
    theme_cls = ThemeManager()

    def build(self):
        kv = Builder.load_file("kivy.kv")
        sm = WindowManager()

        screens = [Information(name="information")]
        for screen in screens:
            sm.add_widget(screen)

        sm.current = "information"
        return sm


if __name__ == '__main__':
    MyApp().run()