【问题标题】:sm.current does not switch screen and gives me attribute error : 'str' object has no attribute 'current'sm.current 不切换屏幕并给我属性错误:“str”对象没有属性“current”
【发布时间】:2021-06-19 10:53:42
【问题描述】:

我是 Kivy 的新手,我正在尝试构建登录屏幕,但出现如下错误。有人可以帮助了解发生了什么问题以及需要做什么吗?

AttributeError: 'str' 对象没有属性 'current'

我的 Kivy 文件如下所示

ScreenManager:
    Welcome:
    SelectionOption:


<Welcome>:
    id: welcome
    name: 'welcome'
    manager: 'sm'
    emailInput: emailInput
    passwordInput: passwordInput
    MDScreen :
        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

<SelectionOption>:
    name : 'selection'
    MDLabel:
        text : 'Selection Option'
        font_style : 'Overline'
    MDRoundFlatButton :
        text: 'Login'
        pos_hint : {'center_x':0.5, 'center_y':0.5}
        font_size : 20
        text_color: 0, 0, 0, 0

我的 python 代码如下所示。

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window
from kivy.properties import ObjectProperty

Window.size = (350, 580)


class Welcome(Screen):
    emailInput = ObjectProperty(None)
    passwordInput = ObjectProperty(None)

    def validatelogin(self):
        if self.emailInput.text == "123" and self.passwordInput.text == "123":
            print("????", self.manager)
            self.manager.current = 'selection'


class SelectionOption(Screen):
    pass


class windowManager(ScreenManager):
    pass


class Console(MDApp):

    def build(self):
        return Builder.load_file('../Files/ScreenManager.kv')


if __name__ == '__main__':
    Console().run()

【问题讨论】:

    标签: python python-3.x kivy kivymd


    【解决方案1】:
    manager: 'sm'
    

    我不知道这是做什么用的,但它编码的正是您看到的错误。

    可能只是删除这一行。

    【讨论】:

    • 谢谢@inclement,这有助于解决问题:)
    猜你喜欢
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2020-12-11
    • 2022-09-23
    • 2018-02-08
    • 2018-03-15
    相关资源
    最近更新 更多