【问题标题】:Can I move from one TextBox to another TextBox by pressing the enter button on the keyboard?我可以通过按键盘上的 enter 按钮从一个 TextBox 移动到另一个 TextBox 吗?
【发布时间】:2017-12-04 06:54:51
【问题描述】:

我有两个文件 test.pytest.kv

我想通过按回车键从一个文本框移动到另一个文本框。这如何实现?

test.py

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

Window.size = (500, 330)

class TestScreen(Screen):
    popup = ObjectProperty(None)


class Test(App):
    def build(self):
        self.root = Builder.load_file('test.kv')
        return self.root


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

test.kv

#:kivy 1.10.0
TestScreen:

    GridLayout:
        cols: 2
        padding : 30,30
        spacing: 10, 10
        row_default_height: '40dp'

        Label:
            text: 'Name'

        TextInput:
            id: name

        Label:
            text: 'Class'

        TextInput:
            id: clas


        Button:
            text: 'Ok'

        Button:
            text: 'Cancel'

【问题讨论】:

  • 请列出迄今为止您尝试过的所有步骤。

标签: python python-3.x python-2.7 kivy kivy-language


【解决方案1】:

既然你想使用 Enter 来改变焦点,我猜你不想要多行 texinput。因此,一种选择是使用on_text_validate 事件:

on_text_validate
当用户点击“输入”时,仅在 multiline=False 模式下触发。这也会使文本输入失去焦点。

在您的 kv 文件中,您可以执行以下操作:

#:kivy 1.10.0
TestScreen:

    GridLayout:
        cols: 2
        padding : 30,30
        spacing: 10, 10
        row_default_height: '40dp'

        Label:
            text: 'Name'

        TextInput:
            id: name
            multiline: False
            on_text_validate: clas.focus = True   # <<<<<<<<<<<

        Label:
            text: 'Class'

        TextInput:
            id: clas
            multiline: False

        Button:
            text: 'Ok'

        Button:
            text: 'Cancel'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 2020-03-03
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多