我想做一个登录页面,接受用户名和密码。两个验证后,应显示一条弹出消息,显示消息:“登录成功”,否则显示弹出消息“登录失败”。 我试过这么做,但我不知道哪里出了问题。任何帮助都将不胜感激。我的代码如下(另存为登录名.py)公司名称:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
Builder.load_string("""
<LoginScreen@Login>:
canvas:
Rectangle:
source: 'b.png'
pos: self.pos
size: self.size
Label:
text: "PLEASE LOGIN OR SIGN UP"
center_x: (root.width/2)
top: (root.top/2)+ 200
font_size: 25
TextInput:
id: txtuname
center_x: (root.width/2)
top: (root.top/2)+ 100
size_hint: None,None
multiline: False
hint_text: "username"
size: 250, 40
max_lines: 1
valign: 'middle'
halign: 'center'
on_text_validate: root.validate();
TextInput:
id: txtpswd
multiline: False
center_x: (root.width/2)
top: (root.top/2)+ 50
size_hint: None,None
hint_text: "password"
size: 250, 40
max_lines: 1
valign: 'middle'
halign: 'center'
on_text_validate: root.validate();
password: True
Button:
id: btnlogin
size: 90,35
pos: 300, 250
font_size: 18
background_color: (1,1,1,0.1)
text: "Login"
on_press: root.validate(txtuname.text,txtpswd.text)
Button:
text: "Sign Up"
size: 90,35
pos: 400, 250
font_size: 18
background_color: (1,1,1,0.1)
<CustomPopup>:
Button:
id: btnpopup
text: "Login successfull"
size_hint: .5, .5
on_press: root.dismiss()
""")
class LoginScreen(Widget):
def validate(self,txtuname,txtpswd):
if txtuname == "username" and txtpswd == "password":
print(txtuname,txtpswd)
b = Button(on_press=self.show_popup)
return b
else:
print("Login failed")
class CustomPopup(Popup):
def show_popup(self, b):
p = CustomPopup()
p.open()
class LoginApp(App):
def build(self):
return LoginScreen()
if __name__ == "__main__":
LoginApp().run()
我是新来的。我想我的代码可以帮你。。在
首先,删除两个
TextInput
的on_text_validate: root.validate();
,因为您要在按按钮登录时进行验证。您可以创建这样一个类作为弹出窗口:然后在你的
^{pr2}$validate()
方法中相关问题 更多 >
编程相关推荐