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

Kivy登录界面开发报错:'str' object has no attribute 'current'问题求助

解决Kivy登录界面的AttributeError问题

嘿,我来帮你搞定这个报错!你遇到的AttributeError: 'str' object has no attribute 'current'是因为在kv文件里给Screen的manager属性赋值错了,咱们一步步来修复:

错误根源

在你的kv文件里,<Welcome>规则下写了manager: 'sm'这行代码——你把manager属性设成了字符串'sm',但Kivy的Screen类的manager属性应该自动关联到它所属的ScreenManager实例,不需要手动赋值成字符串。当你在Python代码里调用self.manager.current时,self.manager是个字符串,自然没有current属性,就触发了这个错误。

另外,你的<Welcome>块里还嵌套了一层MDScreen,这也是不必要的:因为你的Welcome类已经继承自Screen(Python代码里class Welcome(Screen)),所以直接在<Welcome>下定义布局内容就行,嵌套额外的MDScreen会导致布局层级混乱,也可能影响manager的自动绑定。

修复步骤

1. 修正kv文件

  • 删掉<Welcome>块中的manager: 'sm'这一行;
  • 删掉<Welcome>块里最外层的MDScreen :标签,把它的md_bg_color属性直接放到<Welcome>下。

修正后的<Welcome>部分kv代码如下:

<Welcome>:
    id: welcome
    name: 'welcome'
    emailInput: emailInput
    passwordInput: passwordInput
    md_bg_color : [102/255,153/255,153/255,1]
    MDCard :
        size_hint : None, None
        size : 300, 450
        pos_hint : {"center_x":.5,"center_y":.5}
        padding : 20
        spacing : 30
        orientation : "vertical"
        MDLabel :
            text : 'Manager'
            font_style : 'Overline'
            font_size : 30
            halign : "center"
            size_hint_y : None
            height : self.texture_size[1]
            padding_y : 10
        MDTextFieldRound :
            id: emailInput
            hint_text : "Email address"
            icon_right : "account"
            size_hint_x : None
            width : 220
            font_size : 15
            pos_hint : {"center_x":.5}
            color_active : [0.2,0.2,0.1,1]
            TextInput:
                multiline: False
        MDTextFieldRound :
            id: passwordInput
            hint_text : "password"
            icon_right : "eye-off"
            size_hint_x : None
            width : 220
            font_size : 15
            pos_hint : {"center_x":.5}
            color_active : [0.2,0.2,0.1,1]
            password : True
            TextInput:
                multiline: False
        MDRoundFlatButton :
            text : 'Login'
            pos_hint : {'center_x':0.5, 'center_y':0.5}
            font_size : 20
            text_color: 0, 0, 0, 0
            on_release: root.validatelogin()
        MDRoundFlatButton :
            text: 'NEW USER'
            pos_hint: {'center_x':.5, 'center_y':0.4}
            font_size: 20
            text_color: 0, 0, 0, 0
            on_press: root.signup()
        Widget :
            size_hint_y : None
            height : 30

2. 补充未实现的方法(可选)

你的Python代码里Welcome类的signup方法还没实现,为了避免后续报错,可以先加个简单的实现,比如:

def signup(self):
    # 这里可以添加跳转到注册页面的逻辑
    print("Signup button pressed")

3. 验证修复

现在运行代码,当你输入正确的邮箱和密码(123/123)点击登录按钮,应该就能成功跳转到selection屏幕了,不会再出现AttributeError。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 19:47:44