Python/Kivy:使用回车键将一个 TextInput 聚焦到另一个 TextInput

Posted

技术标签:

【中文标题】Python/Kivy:使用回车键将一个 TextInput 聚焦到另一个 TextInput【英文标题】:Python/Kivy : Focus one TextInput to another TextInput using enter key 【发布时间】:2018-09-19 03:21:35 【问题描述】:
    如何使用回车键将焦点从name TextInput 移动到动态添加行的第一列(id:test1)? 在动态rowsecond 列(id:test2) 中按回车键,然后添加新行。添加新行动态时如何聚焦每一行的第一列?

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.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty

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

class User(Screen):

    def add_more(self):
        self.ids.rows.add_row()


class Row(BoxLayout):
    button_text = StringProperty("")


class Rows(BoxLayout):
    row_count = 0

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_row()

    def add_row(self):
        self.row_count += 1
        self.add_widget(Row(button_text=str(self.row_count)))


class Test(App):

    def build(self):
        return self.root


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

test.kv

<Button@Button>:
    font_size: 15
    font_name: 'Verdana'


<TextInput@TextInput>:
    font_size: 15
    font_name: 'Verdana'
    padding_y: 3


<Row>:
    size_hint_y: None
    height: self.minimum_height
    height: 40

    Button:
        text: root.button_text
        size_hint_x: None
        top: 200

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

    TextInput:
        id:test2
        text: ' '
        width: 300
        multiline: False
        on_text_validate: app.root.add_more()

<Rows>:
    size_hint_y: None
    height: self.minimum_height
    orientation: "vertical"

User:
    BoxLayout:
        orientation: "vertical"
        GridLayout:
            cols: 2
            padding: 20, 20
            spacing: 10, 10

            Label:
                text: "Name"
                text_size: self.size
                valign: 'middle'
            TextInput:
                id:name
                multiline: False
                text_size: self.size

        ScrollView:
            Rows:
                id: rows

【问题讨论】:

【参考方案1】:

我添加了以下内容:

    Clock.schedule_once 确保已定义 ID 并将焦点设置在名称 (TextInput) 上。 ObjectProperty 因为

通常认为使用 ObjectProperty 是“最佳实践”。 这会创建一个直接引用,提供更快的访问速度并且更 明确的。

Programming Guide » Kv language » Referencing Widgets

示例

main.py

​​>
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ObjectProperty
from kivy.clock import Clock

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


class User(Screen):
    name = ObjectProperty(None)
    rows = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        Clock.schedule_once(self.set_name_focus, 1)

    def set_name_focus(self, *args):
        self.name.focus = True

    def on_enter_text_input(self):
        self.rows.row.test1.focus = True

    def add_more(self):
        self.rows.add_row()


class Row(BoxLayout):
    button_text = StringProperty("")


class Rows(BoxLayout):
    row_count = 0
    row = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_row()

    def add_row(self):
        self.row_count += 1
        self.row = Row(button_text=str(self.row_count))
        self.add_widget(self.row)


class Test(App):

    def build(self):
        return self.root


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

test.kv

#:kivy 1.10.0

<Button@Button>:
    font_size: 15
    font_name: 'Verdana'


<TextInput@TextInput>:
    font_size: 15
    font_name: 'Verdana'
    padding_y: 3


<Row>:
    test1: test1
    size_hint_y: None
    height: self.minimum_height
    height: 40

    Button:
        text: root.button_text
        size_hint_x: None
        top: 200

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

    TextInput:
        id:test2
        text: ' '
        width: 300
        multiline: False
        on_text_validate: app.root.add_more()

<Rows>:
    size_hint_y: None
    height: self.minimum_height
    orientation: "vertical"

User:
    name: name
    rows: rows
    BoxLayout:
        orientation: "vertical"
        GridLayout:
            cols: 2
            padding: 20, 20
            spacing: 10, 10

            Label:
                text: "Name"
                text_size: self.size
                valign: 'middle'
            TextInput:
                id:name
                multiline: False
                text_size: self.size
                on_text_validate: root.on_enter_text_input()

        ScrollView:
            Rows:
                id: rows

输出

【讨论】:

将此添加到 KV 文件中的每个 TextInput 小部件:codewrite_tab: False

以上是关于Python/Kivy:使用回车键将一个 TextInput 聚焦到另一个 TextInput的主要内容,如果未能解决你的问题,请参考以下文章

Python / Kivy:.kv 文件中的条件设计

使用 Python/Kivy 构建按钮时如何实现线程以及 Buttonpress 的单独线程

Python之深入解析如何使用Python Kivy实现一个“乒乓球”游戏

Python Kivy 无法从另一个类访问 id

Python+kivy+SQLite:如何一起使用

Python Kivy 将文件写入/读取到 SD 卡