Kivy开发TOTP认证器遇TypeError: NoneType不可下标访问错误求助
问题解决:TOTP认证器应用的TypeError错误
错误原因分析
这个TypeError: 'NoneType' object is not subscriptable错误是Kivy的KV语言解析器遇到格式错误导致的,和你替换空格为制表符的操作无关,具体问题出在KV字符串的缩进和语法细节上。
具体修复点
1. 修正MDNavigationDrawer的缩进错误
原代码中MDNavigationDrawer的radius属性错误缩进在id: nav_drawer下方,破坏了KV语言的层级结构,需要调整为和id同级:
MDNavigationDrawer: id: nav_drawer radius: (0, 16, 16, 0) # 与id同级,取消错误缩进 MDNavigationDrawerMenu: ...
2. 闭合MDNavigationDrawerHeader的text字段引号
原代码中text: "Built by simpleuserdontwatch ;) 缺少闭合的双引号,导致解析器无法识别属性边界,补充后应为:
text: "Built by simpleuserdontwatch ;)"
3. 修正后的完整KV代码
<DrawerClickableItem@MDNavigationDrawerItem> focus_color: "#e7e4c0" text_color: "#4a4939" icon_color: "#4a4939" ripple_color: "#c5bdd2" selected_color: "#0c6c4d" <DrawerLabelItem@MDNavigationDrawerItem> text_color: "#4a4939" icon_color: "#4a4939" focus_behavior: False selected_color: "#4a4939" _no_ripple_effect: True MDBoxLayout: orientation: "vertical" pos: 0,0 size_hint: 1,1 spacing: "12dp" MDTopAppBar: title: "MDTopAppBar" left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]] MDScreenManager: MDScreen: MDScrollView: MDRectangleFlatIconButton: text: "Hello!" icon: "key" size_hint: 1,0.1 MDNavigationDrawer: id: nav_drawer radius: (0, 16, 16, 0) MDNavigationDrawerMenu: MDNavigationDrawerHeader: title: "Auther" title_color: "#4a4939" text: "Built by simpleuserdontwatch ;)" spacing: "4dp" padding: "12dp", 0, 0, "56dp" DrawerClickableItem: icon: "settings" text_right_color: "#4a4939" text: "Settings"
额外提示
- Kivy的KV语言对缩进要求严格,建议统一使用4个空格作为缩进单位,避免层级混乱
- 字符串属性的引号必须成对出现,防止解析器无法正确识别属性内容
内容的提问来源于stack exchange,提问作者Kirill
相关产品推荐
相关产品推荐

