kivy - Kivy:如何设置下一个输入框的焦点?
问题描述
我想创建一个登录屏幕,但是将焦点设置在第二个输入框上时遇到问题。当我填写用户名并回车时,密码字段永远不会获得焦点,所以我无法填写密码,永远......我没有看到问题..
谁能帮我吗?
这是我的 signin.kv 文件:
<FlatButton@ButtonBehavior+Label>:
font_size: 16
<SigninWindow>:
id: main_win
orientation: "vertical"
spacing: 10
space_x: self.size[0]/3
canvas.before:
Color:
rgba: (1,1,1, 1)
Rectangle:
size: self.size
pos: self.pos
BoxLayout:
size_hint_y: None
height: 50
canvas.before:
Color:
rgba: (.06, .45, .45, 1)
Rectangle:
size: self.size
pos: self.pos
Label:
text: "Access Control"
bold: True
size_hint_x: .9
FlatButton:
text: "x"
size_hint_x: .1
BoxLayout:
orientation: 'vertical'
padding: main_win.space_x, 10
#spacing: 20
BoxLayout:
orientation: "vertical"
spacing: 10
size_hint_y: None
height: 100
Label:
id: info
text: ''
markup: True
size_hint_y: None
height: 20
TextInput:
id: username_field
hint_text: "Username"
multiline: False
focus: True
on_text_validate: pwd_field.focus = True
TextInput:
id: pwd_field
hint_text: "Password"
multiline: False
password: True
on_text_validate: root.validate_user()
Label:
id: sp
size_hint_y: None
height: 40
Button:
text: "Sign In"
size_hint_y: None
height: 40
background_color: (.06,.45,.45, 1)
background_normal: ''
on_release: root.validate_user()
Label:
id: sp2
这是我的 signin.py 文件:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class SigninWindow(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def validate_user(self):
user = self.ids.username_field
pwd = self.ids.pwd_field
info = self.ids.info
uname = user.text
passw = pwd.text
if uname == '' or passw == '':
info.text = '[color=#FF0000]username and/ or password required[/color]'
else:
if uname == 'admin' and passw == 'admin':
info.text = '[color=#00FF00]Logged In successfully!!![/color]'
else:
info.text = '[color=#FF0000]Invalid Username and/or Password[/color]'
class SigninApp(App):
def build(self):
return SigninWindow()
if __name__=="__main__":
sa = SigninApp()
sa.run()
解决方案
TextInput:
id: username_field
hint_text: "Username"
multiline: False
focus: True
on_text_validate: password_field.focus = True
TextInput:
id: password_field
hint_text: "Password"
multiline: False
password: True
推荐阅读
- javascript - 将谷歌图表保存到图像中并将它们打开到新标签
- swift - iPad 上的 DateFormatter 崩溃,适用于 iPhone
- tfs - 有没有办法为 TFS 设置参数化仪表板(除了团队仪表板)?
- c++ - OMNeT++:未定义对“用于 inet::IMobility 的 typeinfo”的引用
- ms-access - 如何允许文本框显示已存储在 Access 2016 表中的建议值
- javascript - 由于滚动菜单消失,使用 flexbox 的固定元素在 iPhone 移动 Safari 上重新定位
- three.js - 如何在 Angular 应用程序中使用自定义着色器?
- python-3.x - Jupyter 认证问题
- python - Pandas:有没有办法将列中的值组合在一起?
- vba - 在 R1C1 公式中使用时,带小数的变量会转换为 2 位数字