Kivy开发前台工具平台遇AttributeError:类已定义属性却提示不存在
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

