我想使用 KivyMD 和 ScreenManager 创建一个登录和注销页面。但是它不断出错。 在 KivyMD 的文档中,仅显示了如何使用应用程序类创建它。但是,我的目标是使用屏幕管理器来管理两个屏幕登录和注销,功能是当我按下登录时,它应该进入注销页面并在第二个屏幕上显示电子邮件和密码。当我按下注销时,它应该返回登录屏幕。但它是抛出运行时
typeerro
error:
Traceback (most recent call last):
File "f:\PycharmProjects\kivyProject1\login_form.py", line 101, in <module>
MOgggg().run()
File "C:\Users\User\anaconda3\lib\site-packages\kivy\app.py", line 955, in run
self._run_prepare()
File "C:\Users\User\anaconda3\lib\site-packages\kivy\app.py", line 925, in _run_prepare
root = self.build()
File "f:\PycharmProjects\kivyProject1\login_form.py", line 90, in build
kk=Builder.load_string(KV)
File "C:\Users\User\anaconda3\lib\site-packages\kivy\lang\builder.py", line 372, in load_string
parser = Parser(content=string, filename=fn)
File "C:\Users\User\anaconda3\lib\site-packages\kivy\lang\parser.py", line 483, in __init__
self.parse(content)
File "C:\Users\User\anaconda3\lib\site-packages\kivy\lang\parser.py", line 593, in parse
objects, remaining_lines = self.parse_level(0, lines)
File "C:\Users\User\anaconda3\lib\site-packages\kivy\lang\parser.py", line 696, in parse_level
_objects, _lines = self.parse_level(
File "C:\Users\User\anaconda3\lib\site-packages\kivy\lang\parser.py", line 696, in parse_level
_objects, _lines = self.parse_level(
File "C:\Users\User\anaconda3\lib\site-packages\kivy\lang\parser.py", line 756, in parse_level
if current_property[:3] == 'on_':
TypeError: 'NoneType' object is not subscriptable
from kivy.lang import Builder
from kivymd.app import MDApp
# from kivymd.uix.screen import Screen
from kivy.uix.screenmanager import Screen,ScreenManager
from kivymd.uix.button import MDRaisedButton
from kivymd.uix.textfield import MDTextField
from kivymd.uix.label import MDLabel
from kivy.config import Config
#set window size
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '400')
KV="""
ScreenManager:
LoginScreen:
LogoutScreen:
<LoginScreen>:
name: "login"
MDBoxLayout:
orientation:"vertical"
padding: 180
spacing: 10
MDTextField:
id: user_name
hint_text:"Enter Username or Email"
helper_text: "username must be unique"
helper_text_mode:"on_focus"
icon_left:"account"
pos_hint:{"center_x":0.5,"center_y":0.5}
size_hint:(None,None)
width:300
MDTextField:
id: password
hint_text:"Password [8-20 words]"
helper_text: "Password must contain number, spacial char"
helper_text_mode:"on_focus"
icon_left:"lock-off"
pos_hint:{"center_x":0.5,"center_y":0.5}
size_hint:(None,None)
width:300
MDRaisedButton:
text:"Login"
pos_hint:{"center_x":0.5,"center_y":0.5}
on_release:
app.submit()
app.root.current="logout"
root.manager.transition.direction = "left"
<LogoutScreen>:
name:"logout"
BoxLayout:
orientation: 'vertical'
MDLabel:
id: display_label
text: ''
theme_text_color: "Secondary"
halign: 'center'
font_style: "H6"
size_hint_y: None
height: self.texture_size[1]
MDRaisedButton:
text: "LogOut"
pos_hint:{"center_x":0.5,"center_y":0.5}
on_release:
app.root.current="login"
root.manager.transition.direction = "right"
"""
class LogoutScreen(Screen):
pass
class LoginScreen(Screen):
pass
class ScreenManager(ScreenManager):
pass
class MOgggg(MDApp):
def build(self):
kk=Builder.load_string(KV)
return kk
def submit(self):
email=self.root.ids.user_name.text
paasswod=self.root.ids.password.text
self.root.ids.display_label.text=f"Hi {email} , You'r successfully Login!\n and this is your passwrd: {paasswod}"
print(email,paasswod)
MOgggg().run()
您收到的错误是由于
kv
字符串的格式错误造成的。在LogoutScreen
的规则中,MDLabel:
之后的缩进太多。 kv
语言对缩进非常敏感。只需更改:
MDLabel:
id: display_label
text: ''
theme_text_color: "Secondary"
halign: 'center'
font_style: "H6"
size_hint_y: None
height: self.texture_size[1]
至:
MDLabel:
id: display_label
text: ''
theme_text_color: "Secondary"
halign: 'center'
font_style: "H6"
size_hint_y: None
height: self.texture_size[1]