我可以从 Python 触发在 KV 文件中创建的屏幕吗?
Posted
技术标签:
【中文标题】我可以从 Python 触发在 KV 文件中创建的屏幕吗?【英文标题】:Can I trigger a screen created in KV file from Python? 【发布时间】:2020-11-11 17:40:18 【问题描述】:您好,
我实际上想要做的是:
我想创建这个登录屏幕,如果你有正确的邮件(在这种情况下只有 test@test.com)来触发来自 KV 文件的 inapp 屏幕,这是我的实际问题。
我无法触发“inapp”屏幕
我在 PY 文件中没有我的屏幕管理器和屏幕类的原因是它出现了错误,并且当我切换屏幕时它重叠 Here is an example of the buggy UI
我做错了什么?
这是我的 PY 文件:
class mytest(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.accent_palette = 'Blue'
self.theme_cls.accent_hue = '300'
self.theme_cls.primary_palette = "Green"
self.theme_cls.primary_hue = "400"
self.icon = 'testphoto.png'
self.title = "coolapp"
def login_checker(self, mail, psswrd):
hismail = mail
hispass = psswrd
print(themail)
print(thepass)
access_screen = self.root.ids.inapp
if themail == "test@test.com":
print("Corect")
access_screen
else:
toast("Invalid E-mail")
这是KV文件:
BoxLayout:
ScreenManager:
id: scrin_meneger
Screen: #LOGIN
name: "login"
id: loginn
FloatLayout:
orientation: 'vertical'
Image: #Login Background Image
source: "./pictures/whitebg.jpg"
allow_stretch: True
keep_ratio: False
MDTextField: #mail-input
id: emadress
pos_hint: 'center_x': .5, 'center_y': .6
size_hint: 0.60, 0.06
hint_text: "Email: "
current_hint_text_size: 0.5, 1
current_hint_text_color: 0, 0, 0, .60
helper_text: "No input provided"
helper_text_mode: "on_error"
color_mode: 'custom'
line_color_focus: 0, 0, 0, 1
required: True
max_text_lenght: 20
#min_text_lenght: 5
MDTextField: #pass-input
id: psswd
pos_hint: 'center_x': .5, 'center_y': .5
size_hint: 0.60, 0.06
password: True
hint_text: "Password: "
current_hint_text_color: 0, 0, 0, .60
helper_text: "No input provided"
helper_text_mode: "on_error"
color_mode: 'custom'
line_color_focus: 0, 0, 0, 1
required: True
max_text_lenght: 20
#min_text_lenght: 5
MDFlatButton: #Login Button
text: "Login"
text_color: 0, 0, 0, .80
pos_hint: 'center_x': .3, 'y': .2
size_hint: 0.4, 0.1
on_release:
app.login_checker(emadress.text, psswd.text)
Screen: #Application
name: 'inapp'
id: inapp
BoxLayout:
orientation: 'vertical'
MDBottomNavigation:
#Screen 1
MDBottomNavigationItem:
id: main_screen
name: "main_menu"
text: "Main Menu"
icon: "air-humidifier"
BoxLayout:
orientation: 'vertical'
MDToolbar:
id: toolbar
title: "This is my MDToooolbar"
【问题讨论】:
【参考方案1】:你只需要访问ScreenManager
来设置current
Screen
,像这样:
if themail == "test@test.com":
print("Corect")
self.root.ids.scrin_meneger.current = 'inapp'
else:
toast("Invalid E-mail")
【讨论】:
我太笨了。非常感谢约翰的回答!以上是关于我可以从 Python 触发在 KV 文件中创建的屏幕吗?的主要内容,如果未能解决你的问题,请参考以下文章