Python/Kivy:使用Enter键提交表单 [英] Python/Kivy : Form submit using enter key

查看:143
本文介绍了Python/Kivy:使用Enter键提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用enter键从一个TextInput移到另一个TextInput.
我想使用enter键从最后一个TextInput移到Button.当我再次按Enter键时,应调用root.abc()函数.
有人可以告诉我该怎么做吗?

I am moving from one TextInput to another TextInput using enter key.
I want to move from last TextInput to Button using enter key .When I press again enter key then should be call root.abc() function.
Can someone tell me how to done it?

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (300, 100)

class User(Screen):

    def abc(self):
        print('Test')

class Test(App):

    def build(self):
        return self.root

if __name__ == '__main__':
    Test().run()

test.kv

User:
    BoxLayout:
        orientation: "vertical"

        TextInput:
            id:test1
            focus : True
            text: ' '
            width: 100
            multiline: False
            on_text_validate: test2.focus = True

        TextInput:
            id:test2
            text: ' '
            width: 100
            multiline: False
            on_text_validate: test3.focus = True

        Button:
            id:test3
            text: 'Ok'
            on_press : root.abc()

推荐答案

在这里,我们使用向下键绑定,检查按钮的焦点并按下Enter键.

Here we are using a key down binding, check for button's focus and Enter key pressed.

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window
from kivy.properties import ObjectProperty

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (300, 100)


class User(Screen):
    test3 = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        Window.bind(on_key_down=self._on_keyboard_down)

    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
        if self.test3.focus and keycode == 40:  # 40 - Enter key pressed
            self.abc()

    def abc(self):
        print('Test')


class Test(App):

    def build(self):
        return self.root


if __name__ == '__main__':
    Test().run()

test.kv

#:kivy 1.10.0

User:
    test3: test3
    BoxLayout:
        orientation: "vertical"

        TextInput:
            id:test1
            focus : True
            text: ' '
            width: 100
            multiline: False
            on_text_validate: test2.focus = True

        TextInput:
            id:test2
            text: ' '
            width: 100
            multiline: False
            on_text_validate:
                test3.background_normal = ''
                test3.background_color = [0, 0, 1, 0.5]    # 50% translucent blue
                test3.focus = True

        Button:
            id:test3
            text: 'Ok'
            focus: False
            on_press : root.abc()

输出

Output

这篇关于Python/Kivy:使用Enter键提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆