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

Kivy开发前台工具平台遇AttributeError:类已定义属性却提示不存在

Fixing the AttributeError in Your Kivy MasterControl Class

Hey there, let's sort out that AttributeError you're facing when assigning a user to your session. The issue is actually a common Python gotcha with class instance variables!

What's Going Wrong?

Looking at your MasterControl class, in the __init__ method you've declared m_currentUser = None as a local variable instead of an instance attribute. That means it only exists inside the __init__ method, not on the actual MasterControl object itself. When you try to set self.m_currentUser = emp in loginUser(), Python can't find that attribute on the instance, hence the error.

The Fix

All you need to do is prefix m_currentUser with self. in the __init__ method to bind it to the instance:

from kivy.uix.boxlayout import BoxLayout

class MasterControl(BoxLayout):
    def __init__(self, **kwargs):
        super(MasterControl, self).__init__(**kwargs)
        # Use self. to make this an instance attribute
        self.m_currentUser = None

    def loginUser(self, emp):
        self.m_currentUser = emp
        print("I made it to login!")
        # Rest of your UI update logic here...

Why This Works

By using self.m_currentUser, you're telling Python to store this variable as part of the MasterControl instance. Now when you access self.m_currentUser in loginUser(), it refers to the same attribute that was initialized in __init__, so no more AttributeError.

Quick side note: If you're using Python 3 (which you should be for modern Kivy development), print is a function—so I added parentheses around the string in the fixed code to keep it compatible.

内容的提问来源于stack exchange,提问作者Scott Glaittli

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:29:40