Python/Kivy:使用键盘上下键将光标移动到动态添加行中

Python/Kivy:使用键盘上下键将光标移动到动态添加行中,python,python-2.7,kivy-language,Python,Python 2.7,Kivy Language,我正在使用python-2.7和kivy。当我点击+添加更多时,行将被添加动态。有人帮助我如何使用键盘将光标一行跳到另一行? 如果光标进入第二行,第二列。如果我按向上键,则光标应跳入第1行,第二列 若我按下向下键,则光标应跳入第三行,第二列 test.py 试验电压(千伏) : 方向:“水平” 按钮: 文本:root.button\u文本 大小\u提示\u x:.2 文本输入: 大小\u提示\u x:.8 文本输入: 大小\u提示\u x:.8 显示: 盒子布局: 方向:“垂直” 盒子布局: 方

我正在使用python-2.7和kivy。当我点击
+添加更多
时,行将被添加动态。有人帮助我如何使用键盘将光标
一行
跳到
另一行

如果光标进入
第二行
第二列
。如果我按
向上
键,则光标应跳入
第1行
第二列
若我按下
向下
键,则光标应跳入
第三行
第二列

test.py 试验电压(千伏)
:
方向:“水平”
按钮:
文本:root.button\u文本
大小\u提示\u x:.2
文本输入:
大小\u提示\u x:.8
文本输入:
大小\u提示\u x:.8
显示:
盒子布局:
方向:“垂直”
盒子布局:
方向:“水平”
按钮:
大小\u提示\u x:.2
正文:“+添加更多”
valign:“底部”
按:root.add\u more()
盒子布局:
方向:“水平”
排:
id:行

解决方案如下。有关详细信息,请参阅代码片段示例

片段 请修改此功能: 添加新功能 例子 test.py 试验电压(千伏)
:
方向:“水平”
按钮:
文本:root.button\u文本
大小\u提示\u x:.2
文本输入:
大小\u提示\u x:.8
文本输入:
大小\u提示\u x:.8
显示:
盒子布局:
方向:“垂直”
盒子布局:
方向:“水平”
按钮:
大小\u提示\u x:.2
正文:“+添加更多”
valign:“底部”
按:root.add\u more()
盒子布局:
方向:“水平”
排:
id:行
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

Window.size = (450, 325)


class display(Screen):

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


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


class Rows(BoxLayout):
    orientation = "vertical"
    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()
<Row>:
    orientation: "horizontal"

    Button:
        text: root.button_text
        size_hint_x: .2

    TextInput:
        size_hint_x: .8

    TextInput:
        size_hint_x: .8


display:

    BoxLayout:
        orientation: "vertical"

        BoxLayout:
            orientation: "horizontal"

            Button:
                size_hint_x: .2
                text: "+Add More"
                valign: 'bottom'
                on_press: root.add_more()


        BoxLayout:
            orientation: "horizontal"

        Rows:
            id: rows
class display(Screen):

    def __init__(self, **kwargs):
        super(display, self).__init__(**kwargs)
        Window.bind(on_key_down=self._on_keyboard_down)

    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
        if int(keycode) == 81 or int(keycode) == 82:
            if int(keycode) == 82:
                key = 'up'
            if int(keycode) == 81:
                key = 'down'

            rows = self.ids.rows
            countRowLen = len(rows.children)
            i = 0
            for row in reversed(rows.children):
                i = i + 1
                j = 0
                for ch in reversed(row.children):
                    if isinstance(ch, TextInput):
                        j = j + 1
                        if ch.focus == True:
                            self.params = {'count': countRowLen, 'row':i, 'column':j, 'key':key, 'gridObj': rows}
                            set_focus_grid_use_keyboard(self)
                            return True
            return True

    def add_more(self):
        self.ids.rows.add_row()
def set_focus_grid_use_keyboard(obj):
    if int(obj.params['count']) == int(obj.params['row']) and obj.params['key'] == 'down':
        focusRow = int(1)
    elif int(obj.params['row']) == 1 and obj.params['key'] == 'up':
        focusRow = int(obj.params['count'])
    elif obj.params['key'] == 'down':
        focusRow =  int(obj.params['row']) + 1
    elif obj.params['key'] == 'up':
        focusRow =  int(obj.params['row']) - 1


    i = 0
    for row in reversed(obj.params['gridObj'].children):
        i += 1
        j = 0
        print(i)
        print(focusRow)
        if i == int(focusRow):
            for ch in reversed(row.children):
                if isinstance(ch, TextInput):
                    j += 1
                    if j == int(obj.params['column']):
                        ch.focus = True
                        return False
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
from kivy.uix.textinput import TextInput

Window.size = (450, 325)


def set_focus_grid_use_keyboard(obj):
    if int(obj.params['count']) == int(obj.params['row']) and obj.params['key'] == 'down':
        focusRow = int(1)
    elif int(obj.params['row']) == 1 and obj.params['key'] == 'up':
        focusRow = int(obj.params['count'])
    elif obj.params['key'] == 'down':
        focusRow =  int(obj.params['row']) + 1
    elif obj.params['key'] == 'up':
        focusRow =  int(obj.params['row']) - 1


    i = 0
    for row in reversed(obj.params['gridObj'].children):
        i += 1
        j = 0
        print(i)
        print(focusRow)
        if i == int(focusRow):
            for ch in reversed(row.children):
                if isinstance(ch, TextInput):
                    j += 1
                    if j == int(obj.params['column']):
                        ch.focus = True
                        return False


class display(Screen):

    def __init__(self, **kwargs):
        super(display, self).__init__(**kwargs)
        Window.bind(on_key_down=self._on_keyboard_down)

    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
        if int(keycode) == 81 or int(keycode) == 82:
            if int(keycode) == 82:
                key = 'up'
            if int(keycode) == 81:
                key = 'down'

            rows = self.ids.rows
            countRowLen = len(rows.children)
            i = 0
            for row in reversed(rows.children):
                i = i + 1
                j = 0
                for ch in reversed(row.children):
                    if isinstance(ch, TextInput):
                        j = j + 1
                        if ch.focus == True:
                            self.params = {'count': countRowLen, 'row':i, 'column':j, 'key':key, 'gridObj': rows}
                            set_focus_grid_use_keyboard(self)
                            return True
            return True

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


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


class Rows(BoxLayout):
    orientation = "vertical"
    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()
<Row>:
    orientation: "horizontal"

    Button:
        text: root.button_text
        size_hint_x: .2

    TextInput:
        size_hint_x: .8

    TextInput:
        size_hint_x: .8


display:

    BoxLayout:
        orientation: "vertical"

        BoxLayout:
            orientation: "horizontal"

            Button:
                size_hint_x: .2
                text: "+Add More"
                valign: 'bottom'
                on_press: root.add_more()


        BoxLayout:
            orientation: "horizontal"

        Rows:
            id: rows