Python / Kivy:从动态添加行中输入一个TextBox到另一个TextBox

时间:2018-04-06 17:23:10

标签: python-2.7 kivy kivy-language

我正在使用Python-2.7和kivy.Now我正在动态添加row点击+添加更多按钮。
有人可以告诉我如何通过输入密钥添加行动态当enter进入每个TextBox的最后row而不是新增的第一列上的+添加更多按钮和光标focus时行?

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, 400)

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

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

User:
    BoxLayout:
        orientation: "vertical"
        padding : 20, 5

        ScrollView:
            Rows:
                id: rows


        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .2
            size_hint_y: .2

            Button:
                text: "+Add More"
                on_press: root.add_more()

1 个答案:

答案 0 :(得分:2)

解决方案

  1. 删除 +添加更多按钮
  2. 焦点:True 添加到第一个 TextInput,test1
  3. 将回调 on_text_validate:app.root.add_more()添加到第二个 TextInput,test2
  4. test.kv

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

    输出

    Img01 - AppStartup Img02 - Input Img03 - New Row Added after user pressed ENTER