【问题标题】:Python/Kivy : Focus one TextInput to another TextInput using enter keyPython/Kivy:使用回车键将一个 TextInput 聚焦到另一个 TextInput
【发布时间】:2018-09-19 03:21:35
【问题描述】:
  1. 如何使用回车键将焦点从name TextInput 移动到动态添加行的第一列(id:test1)
  2. 在动态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

【问题讨论】:

    标签: python-2.7 kivy kivy-language


    【解决方案1】:

    我添加了以下内容:

    1. Clock.schedule_once 确保已定义 ID 并将焦点设置在名称 (TextInput) 上。
    2. 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
    猜你喜欢
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    相关资源
    最近更新 更多