我正在尝试Kivymd,但始终收到错误:AttributeError:'NoneType'对象没有属性'theme_cls'。
我在App类中添加了theme_cls = ThemeManager(),但仍然出现错误。由于没有更多的在线解决方案,请提供帮助。
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition, WipeTransition
from kivy.app import App
from kivy.uix.button import Button
from kivymd.navigationdrawer import MDNavigationDrawer
from kivymd.theming import ThemeManager
class BeginScreen(Screen):
pass
class MyScreenManager(ScreenManager):
pass
Login = Builder.load_string('''
#: import Toolbar kivymd.toolbar.Toolbar
MyScreenManager:
Screen:
<Screen>:
Toolbar:
title: "Simple toolbar"
pos_hint: {'center_x': 0.5, 'center_y': 0.75}
md_bg_color: get_color_from_hex(colors['Teal']['500'])
background_palette: 'Teal'
background_hue: '500'
Toolbar:
title: "Toolbar with right buttons"
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
md_bg_color: get_color_from_hex(colors['Amber']['700'])
background_palette: 'Amber'
background_hue: '700'
right_action_items: [['content-copy', lambda x: None]]
Toolbar:
title: "Toolbar with left and right buttons"
pos_hint: {'center_x': 0.5, 'center_y': 0.25}
md_bg_color: get_color_from_hex(colors['DeepPurple']['A400'])
background_palette: 'DeepPurple'
background_hue: 'A400'
left_action_items: [['arrow-left', lambda x: None]]
right_action_items: [['lock', lambda x: None], \
['camera', lambda x: None], \
['play', lambda x: None]]
''')
class MyApp(App):
theme_cls = ThemeManager()
def build(self):
return Login
if __name__ == "__main__":
MyApp().run()
答案 0 :(得分:1)
当Kivy处理kv文件/字符串时,它在实例化MyScreenManager:
之前实例化了根规则 theme_cls
。实例化MyScreenManager:
时,实例化其子节点Screen:
。实例化Screen:
时,实例化了引用Toolbar:
的子节点theme_cls
。但是theme_cls
尚未实例化。因此,它引发了以下错误,
self._shadow = App.get_running_app().theme_cls.quad_shadow
AttributeError:“ NoneType”对象没有属性“ theme_cls”
MyScreenManager:
替换根规则<MyScreenManager>:
Login = Builder.load_string('''
#:import Toolbar kivymd.toolbar.Toolbar
<MyScreenManager>:
Screen:
return Login
替换为return MyScreenManager()
class MyApp(App):
theme_cls = ThemeManager()
def build(self):
return MyScreenManager()