【问题标题】:Python/Kivy : Move one TextBox to another TextBox from enter in dynamic add rowPython/Kivy:将一个文本框从动态添加行中的输入移动到另一个文本框
【发布时间】:2018-04-06 17:23:09
【问题描述】:

我正在使用 Python-2.7 和 kivy。现在我正在做动态添加 row 点击+添加更多按钮。
有人可以告诉我如何通过回车键添加行动态当enter 进入每个row 的最后一个TextBox 而不是在新行的第一列上添加更多按钮和光标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()

【问题讨论】:

    标签: python-2.7 kivy kivy-language


    【解决方案1】:

    解决方案

    1. 移除+添加更多按钮
    2. focus: True 添加到第一个 TextInput, test1
    3. 添加回调,on_text_validate: app.root.add_more() 到第二个TextInput, test2

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

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多