[英]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.