简体   繁体   English

如何修复AttributeError:'NoneType'对象没有属性'theme_cls'

[英]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()

Root Cause 根本原因

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”

Solution

kv string kv字符串

  • Replace root rule, MyScreenManager: with class rule, <MyScreenManager>: 将根规则MyScreenManager:替换为类规则<MyScreenManager>:

Snippets 片段

Login = Builder.load_string('''
#:import Toolbar kivymd.toolbar.Toolbar


<MyScreenManager>:
    Screen:

Python script Python脚本

  • Replace return Login with return MyScreenManager() return MyScreenManager()替换return Login

Snippets 片段

class MyApp(App):
    theme_cls = ThemeManager()

    def build(self):
        return MyScreenManager()

Output 产量

结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 KivyMD: AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;theme_cls&#39; - KivyMD: AttributeError: 'NoneType' object has no attribute 'theme_cls' KivyMD: AttributeError: 'NoneType' object 在添加 MDToolbar 时没有属性 'theme_cls' - KivyMD: AttributeError: 'NoneType' object has no attribute 'theme_cls' upon Adding MDToolbar Kivymd 错误 self._shadow = App.get_running_app().theme_cls.quad_shadow AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;theme_cls&#39; - Kivymd error self._shadow = App.get_running_app().theme_cls.quad_shadow AttributeError: 'NoneType' object has no attribute 'theme_cls' “如何解决&#39;AttributeError:&#39;NoneType&#39;对象在Python中没有属性&#39;tbody&#39;错误? - "How to fix 'AttributeError: 'NoneType' object has no attribute 'tbody'' error in Python? 如何修复 AttributeError: 'NoneType' object has no attribute 'strip' in this specific code? - how to fix AttributeError: 'NoneType' object has no attribute 'strip' in this specific code? 我该如何解决这个 AttributeError: 'NoneType' object has no attribute 'text'? - How do i fix this AttributeError: 'NoneType' object has no attribute 'text'? 如何修复 AttributeError: 'NoneType' object 在 insta bot 中没有属性 'click' - How to fix AttributeError: 'NoneType' object has no attribute 'click' in insta bot 我该如何修复,AttributeError: 'NoneType' 对象没有属性 'send' - How can I fix, AttributeError: 'NoneType' object has no attribute 'send' 如何修复 AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;click&#39; - How to fix AttributeError: 'NoneType' object has no attribute 'click' 如何修复“”AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;app&#39; - How to fix ""AttributeError: 'NoneType' object has no attribute 'app'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM