You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KivyMD开发中出现AttributeError: 'NoneType' object has no attribute 'update_items_color'错误的求助

KivyMD开发中出现AttributeError: 'NoneType' object has no attribute 'update_items_color'错误的求助

嘿,我仔细看了你的代码,很快就找到了问题所在!

这个错误的根源是你在KV文件里对导航菜单的结构嵌套错了——你把MDNavigationDrawerDivider和所有的MDNavigationDrawerItem都放在了MDNavigationDrawerHeader组件的内部,这不符合KivyMD的组件层级要求。

在新版KivyMD中,MDNavigationDrawerDivider和MDNavigationDrawerItem应该是MDNavigationDrawerMenu的直接子元素,而不是嵌套在Header里面。这种错误会导致菜单项无法被正确注册到菜单对象中,当你点击Item时,系统尝试调用update_items_color方法来更新选中状态,但对应的菜单实例是None,所以就抛出了这个AttributeError。

只需要调整KV文件中导航菜单的结构,把Divider和Item移到Header外面,和Header同级就可以解决问题了。修改后的导航菜单部分代码如下:

MDNavigationDrawerMenu:
    MDNavigationDrawerHeader:
        orientation:"vertical"
        padding: (0, 0, 0, "12dp")
        adaptive_height: True
        MDLabel: 
            text: "Menu"
            adaptive_height: True
            #padding_x: "16dp"
            font_style: "Display"
            role: "small"
    
    MDNavigationDrawerDivider:
    
    MDNavigationDrawerItem:
        active_indicator_color: "#e7e4c0"
        on_release: root.manager.current = "gender"
        MDNavigationDrawerItemLeadingIcon:
            icon: "book-open-variant"
            theme_icon_color: "Custom"
            icon_color: "#4a4939"
        MDNavigationDrawerItemText:
            text: "Words' Gender"
    
    MDNavigationDrawerItem:
        active_indicator_color: "#e7e4c0"
        MDNavigationDrawerItemLeadingIcon:
            icon: "pencil-plus"
            theme_icon_color: "Custom"
            icon_color: "#4a4939"
        MDNavigationDrawerItemText:
            text: "Personalize Dictionary"
    
    MDNavigationDrawerItem:
        active_indicator_color: "#e7e4c0"
        MDNavigationDrawerItemLeadingIcon:
            icon: "cog"
            theme_icon_color: "Custom"
            icon_color: "#4a4939"
        MDNavigationDrawerItemText:
            text: "Settings"

另外,你之前注释掉的第一个Item的on_release事件也可以恢复了,这样点击它就能正常跳转到Gender页面啦。

备注:内容来源于stack exchange,提问作者staff1515

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 11:17:57