[英]How to fix AttributeError: 'NoneType' object has no attribute 'theme_cls'
I am trying the Kivymd but keep getting the error: AttributeError: 'NoneType' object has no attribute 'theme_cls'. 我正在尝试Kivymd,但始终收到错误:AttributeError:'NoneType'对象没有属性'theme_cls'。
I added the theme_cls = ThemeManager() in the App class but am still getting the error. 我在App类中添加了theme_cls = ThemeManager(),但仍然出现错误。 Please help as there are no more solutions online. 由于没有更多的在线解决方案,请提供帮助。
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()
When Kivy process the kv file / string, it instantiated the root rule , MyScreenManager:
before the instantiation of theme_cls
. 当Kivy处理kv文件/字符串时,它会在theme_cls
实例化之前实例化根规则 MyScreenManager:
theme_cls
When MyScreenManager:
is instantiated, it instantiates its child, Screen:
. 实例化MyScreenManager:
,将实例化其子Screen:
。 When Screen:
is instantiated, it instantiates its children, Toolbar:
which references theme_cls
. 实例化Screen:
,实例化其子对象Toolbar:
引用theme_cls
。 But theme_cls
is not yet instantiated. 但是theme_cls
尚未实例化。 Therefore, it threw the following error, 因此,它引发了以下错误,
self._shadow = App.get_running_app().theme_cls.quad_shadow
AttributeError: 'NoneType' object has no attribute 'theme_cls' AttributeError:“ NoneType”对象没有属性“ theme_cls”
MyScreenManager:
with class rule, <MyScreenManager>:
将根规则MyScreenManager:
替换为类规则<MyScreenManager>:
Login = Builder.load_string('''
#:import Toolbar kivymd.toolbar.Toolbar
<MyScreenManager>:
Screen:
return Login
with return MyScreenManager()
用return MyScreenManager()
替换return Login
class MyApp(App):
theme_cls = ThemeManager()
def build(self):
return MyScreenManager()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.