【问题标题】:KivyMD AttributeError: 'NoneType' object has no attribute 'theme_cls'KivyMD AttributeError:“NoneType”对象没有属性“theme_cls”
【发布时间】:2019-12-23 22:12:48
【问题描述】:

我无法让 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

【问题讨论】:

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


    【解决方案1】:

    您只需要在定义MyApp 类之后执行您的Builder.load_file()。您可以在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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2021-04-03
      • 2019-01-01
      • 2021-12-26
      • 2019-07-23
      相关资源
      最近更新 更多