Kivy:如何将焦点设置在下一个输入框上?

问题描述 投票:0回答:1

我想创建一个登录界面,但是我遇到了一个问题,要把焦点设置在第二个输入框上。当我填入用户名并按下回车键时,密码栏从来没有得到焦点,所以我无法填入密码,从来没有...。我不明白这个问题。

谁能帮帮我?

这是我的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()
kivy focus inputbox
1个回答
0
投票
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
© www.soinside.com 2019 - 2024. All rights reserved.