python - KivyMD 按下按钮时切换屏幕?
问题描述
我正在做一个简单的登录应用程序,我似乎得到了一个
if current_property[:3] == 'on_':
TypeError: 'NoneType' object is not subscriptable
这是我的代码:
ScreenManager:
Password:
Second_Screen:
<Password@Screen>
id: 'main'
MDCard:
size_hint: None, None
size: 600, 800
pos_hint: {'center_x': .5, "center_y": .5}
elevation: 10
padding: 25
spacing: 25
orientation: 'vertical'
FitImage:
source: "pfa2.jpg"
MDLabel:
id: welcome_label
text: "Velkommen til PFRemmøde"
font_size: 40
halign: 'center'
size_hint_y: None
height: self.texture_size[1]
padding_y: 15
MDTextFieldRound:
id: user
hint_text: "Brugernavn"
icon_right: "account"
size_hint_x: None
width: 225
font_size: 20
pos_hint: {"center_x": 0.5}
MDTextFieldRound:
id: password
hint_text: "Password"
icon_right: "eye-off"
size_hint_x: None
width: 225
font_size: 20
pos_hint: {"center_x": 0.5}
password: True
MDRoundFlatButton:
text: "Log ind"
width: 200
font_size: 12
pos_hint: {"center_x": 0.5}
on_release:
app.root.current = "second" if password.text =="1234" else "main"
root.manager.transition.direction = "right"
MDRoundFlatButton:
text: "Afslut"
width: 200
font_size: 12
pos_hint: {"center_x": 0.5}
on_press: app.clear()
<Second_Screen>:
id: 'second'
MDRoundFlatButton:
text: "Log ind"
width: 200
font_size: 12
pos_hint: {"center_x": 0.5}
on_release:
app.root.current = "second" if passw.text == "pswd" else "main"
root.manager.transition.direction = "left"
我运行的是:
class MainApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Red"
return Builder.load_file('login.kv')
def logger(self):
self.root.ids.welcome_label.text = f'Velkommen {self.root.ids.user.text}!'
def clear(self):
self.root.ids.welcome_label.text = "WELCOME"
self.root.ids.user.text = ""
self.root.ids.password.text = ""
MainApp().run()
我错过了一些明显的东西吗?我的理想结果是,如果输入“1234”作为密码,屏幕应该切换到应用程序的“登录”版本。然而,这似乎不起作用......我对 KivyMD 很陌生,所以请多多包涵:-)
解决方案
您的代码存在问题:
您必须为您的
Screens
:ScreenManager: Password: name: 'passw' Second_Screen: name: 'second'
你的
SecondScreen
课程应该扩展Screen
:<Second_Screen@Screen>:
您的
SecondScreen
规则中有不正确的缩进,这会导致您看到的错误。这里是修正版:<Second_Screen@Screen>: id: 'second' MDRoundFlatButton: text: "Log ind" width: 200 font_size: 12 pos_hint: {"center_x": 0.5} on_release: app.root.current = "second" if passw.text == "pswd" else "main" root.manager.transition.direction = "left"
推荐阅读
- python - 在 python 中使用带有 a+ 模式的 open 函数的奇怪结果
- php - 如何仅减慢当前请求的速度?sleep() 没有按预期工作
- ios - Swift 5 - 用户第二次单击标签栏按钮后,需要帮助在视图控制器 1 上重新加载 Web 视图
- sql - Oracle xmlagg 字符串缓冲区太小
- javascript - [Vue 警告]:属性或方法“列表”未在实例上定义,但在渲染期间被引用。确保此属性是反应性的 [...]
- elasticsearch - 在 Logstash if 语句中使用非白名单字段
- python - 以编程方式确定为将每月债务减少 X 所需的最小组合余额
- javascript - Javascript - 遍历一棵树以生成节点的序列
- amazon-s3 - 为多个 s3 文本对象生成 redshift ddls
- javascript - 如何在javascript中运行一个jar文件并让它返回一个json