Python/Kivy : 将一个文本框从动态添加行中的输入移动到另一个文本框
Python/Kivy : Move one TextBox to another TextBox from enter in dynamic add row
我正在使用 Python-2.7 和 kivy.Now 我正在做动态添加 row
点击 +Add More 按钮。
有人能告诉我如何通过输入键动态添加行当 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()
解决方案
- 删除 +添加更多 按钮
- 将focus: True 添加到第一个TextInput, test1
- 添加回调,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()
输出
我正在使用 Python-2.7 和 kivy.Now 我正在做动态添加 row
点击 +Add More 按钮。
有人能告诉我如何通过输入键动态添加行当 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()
解决方案
- 删除 +添加更多 按钮
- 将focus: True 添加到第一个TextInput, test1
- 添加回调,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()