简体   繁体   English

KivyMD:“超级”对象在基于条件的屏幕转换期间没有属性“__getattr__”

[英]KivyMD: 'super' object has no attribute '__getattr__' during screen transition based on condition

I want to switch from login screen to menu screen based on a successful authentication, but this is the best I could do after a long search on condition-based screen transitions.我想根据成功的身份验证从登录屏幕切换到菜单屏幕,但这是在长时间搜索基于条件的屏幕转换后我能做的最好的事情。 Most sites say that screen-transitions in kivymd should be done using 'on-release' in .kv file, but I don't think that it would work in my code.大多数网站都说 kivymd 中的屏幕转换应该使用 .kv 文件中的“on-release”来完成,但我认为它不适用于我的代码。

I've marked on the code the problematic line, which is raising the exception.我在代码上标记了有问题的行,这会引发异常。

Teste.py Teste.py

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton
from kivy.uix.screenmanager import ScreenManager, Screen


class login(Screen):
    pass
class menu(Screen):
    pass

Builder.load_file('lteste.kv')
class LoginApp(MDApp):
    dialog = None
    def build(self): #método construtor da parte visual do aplicativo
        
        self.theme_cls.theme_style = "Light"
        self.theme_cls.primary_palette = "Indigo"
        self.theme_cls.accent_palette = "Blue"

        self.sm = ScreenManager()
        self.sm.add_widget(login(name="login"))
        self.sm.add_widget(menu(name="menu"))
        self.sm.current = "menu"
        return self.sm
    
    def dialog_box(self):
        if not self.dialog:
            self.dialog = MDDialog(
                title="Log In",
                text=f"Welcome {self.root.ids.user.text}!",
                buttons=[MDFlatButton(text="Ok", text_color=self.theme_cls.primary_color,
                                      on_release=self.close),],)
        return self.dialog.open()
    
    def login(self):
        if self.root.ids.user.text=='1' and self.root.ids.password.text=='1':
            self.sm.current = "menu" #<- problem
            self.dialog_box()
            return True
        else:
            return False
    
    def close(self, instance):
        self.dialog.dismiss()
        

LoginApp().run()

lteste.kv lteste.kv


<login>:
    id: login
    name: "login"
    MDCard:
        size_hint: None, None
        size: 300, 600
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        elevation: 10
        padding: 65
        spacing: 35
        orientation: 'vertical'
        MDIcon:
            icon: 'account'
            icon_color: 0, 0, 0, 0
            halign: 'center'
            font_size: 180
        MDTextFieldRound:
            id: user
            icon_left: "account-check"
            hint_text: "Usuário"
            foreground_color: 1, 0, 1, 1
            size_hint_x: None
            width: 220
            font_size: 20
            pos_hint: {"center_x": 0.5}
        MDTextFieldRound:
            id: password
            icon_left: "key-variant"
            hint_text: "Senha"
            foreground_color: 1, 0, 1, 1
            size_hint_x: None
            height: 1
            width: 220
            font_size: 20            
            pos_hint: {"center_x": 0.5}
            password: True
        MDFillRoundFlatButton:
            text: "ENTRAR"
            font_size: 15
            pos_hint: {"center_x": 0.5}
            on_press: app.login()
        MDFillRoundFlatButton:
            text: "REGISTRAR-SE"
            font_size: 15
            pos_hint: {"center_x": 0.5}
<menu>
    name: "menu"
    id: menu
    MDCard:
        size_hint: None, None
        size: 300, 600
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        elevation: 10
        padding: 65
        spacing: 35
        orientation: 'vertical'
        MDRaisedButton:
            text: "Test"

The application root does not contain the ids you want.应用程序root不包含您想要的ids They are in the login screen widget because you defined them under it in kivy language.它们位于login屏幕小部件中,因为您在其下用 kivy 语言定义了它们。 It would help to store a reference on the login screen to access them.这将有助于在登录屏幕上存储参考以访问它们。 Replace代替

        self.sm.add_widget(login(name="login"))

with

        self.login_screen = login(name="login")
        self.sm.add_widget(self.login_screen)

Then, you can access the widgets like so,然后,您可以像这样访问小部件,

                text=f"Welcome {self.login_screen.ids.user.text}!",

and

        if self.login_screen.ids.user.text=='1' \
                and self.login_screen.ids.password.text=='1':

There is an example in the Kivy Documentation on kivy.uix.widget.Widget.ids and also Accessing Widgets defined inside Kv lang in your Python code . Kivy 文档中有一个关于kivy.uix.widget.Widget.ids的示例,还有Accessing Widgets defined inside Kv lang in your Python code

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'super' object 在 kivymd 中没有属性 '__getattr__' - 'super' object has no attribute '__getattr__' in kivymd kivymd: AttributeError: 'super' object 没有属性 '__getattr__' - kivymd: AttributeError: 'super' object has no attribute '__getattr__' 屏幕管理器(AttributeError:“ super”对象没有属性“ __getattr__”) - screen manager (AttributeError: 'super' object has no attribute '__getattr__') 屏幕管理器AttributeError:“超级”对象没有属性“ __getattr__” - Screen manager AttributeError: 'super' object has no attribute '__getattr__' AttributeError:“超级”对象没有属性“ __getattr__” - AttributeError: 'super' object has no attribute '__getattr__' AttributeError: &#39;super&#39; 对象在 kivymd 请求应用程序中没有属性 &#39;__getattr__&#39; 错误 - AttributeError: 'super' object has no attribute '__getattr__' error at kivymd request app 当我尝试访问屏幕 ID 时,“超级”对象没有属性“__getattr__” - 'super' object has no attribute '__getattr__' when I try to access screen ids AttributeError:“超级”对象没有属性“ __getattr __”(我进行了搜索,但无济于事) - AttributeError: 'super' object has no attribute '__getattr__' ( I searched, but to no avail) &#39;super&#39;对象在python3中没有属性&#39;__getattr__&#39; - 'super' object has no attribute '__getattr__' in python3 AttributeError: &#39;super&#39; 对象在 python 中没有属性 &#39;__getattr__&#39; - AttributeError: 'super' object has no attribute '__getattr__' in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM