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

时间:2018-04-09 16:46:18

标签: python-2.7 kivy kivy-language

  1. 如何使用按回车键将焦点从name TextInput移动到动态添加行的第一列(id:test1)
  2. 按下输入动态second的{​​{1}}列(id:test2),然后添加新行。添加新行动态时如何聚焦每一行的第一列?
  3. test.py

    row

    test.kv

    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()
    

1 个答案:

答案 0 :(得分:1)

我添加了以下内容:

  1. Clock.schedule_once以确保定义id并将焦点设置为名称(TextInput)。
  2. ObjectProperty因为
  3.   

    通常认为使用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
    

    输出

    Img01 - App Startup Img02 - test1-TextInput @ Row 1 Img03 - test2-TextInput @ Row 1 Img04 - test1-TextInput @ Row 2