我可以通过按键盘上的 enter 按钮从一个 TextBox 移动到另一个 TextBox 吗?
Can I move from one TextBox to another TextBox by pressing the enter button on the keyboard?
我有两个文件 test.py
和 test.kv
。
我想通过按回车键从一个文本框移动到另一个文本框。如何实现?
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.properties import ObjectProperty
Window.size = (500, 330)
class TestScreen(Screen):
popup = ObjectProperty(None)
class Test(App):
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
Test().run()
test.kv
#:kivy 1.10.0
TestScreen:
GridLayout:
cols: 2
padding : 30,30
spacing: 10, 10
row_default_height: '40dp'
Label:
text: 'Name'
TextInput:
id: name
Label:
text: 'Class'
TextInput:
id: clas
Button:
text: 'Ok'
Button:
text: 'Cancel'
既然您想使用 Enter 来改变焦点,我猜您不想要多行 texinput。因此,一种选择是使用 on_text_validate
事件:
on_text_validate
Fired only in multiline=False mode when the user hits ‘enter’. This will also unfocus the textinput.
在您的 kv 文件中,您可以执行如下操作:
#:kivy 1.10.0
TestScreen:
GridLayout:
cols: 2
padding : 30,30
spacing: 10, 10
row_default_height: '40dp'
Label:
text: 'Name'
TextInput:
id: name
multiline: False
on_text_validate: clas.focus = True # <<<<<<<<<<<
Label:
text: 'Class'
TextInput:
id: clas
multiline: False
Button:
text: 'Ok'
Button:
text: 'Cancel'
我有两个文件 test.py
和 test.kv
。
我想通过按回车键从一个文本框移动到另一个文本框。如何实现?
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.properties import ObjectProperty
Window.size = (500, 330)
class TestScreen(Screen):
popup = ObjectProperty(None)
class Test(App):
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
Test().run()
test.kv
#:kivy 1.10.0
TestScreen:
GridLayout:
cols: 2
padding : 30,30
spacing: 10, 10
row_default_height: '40dp'
Label:
text: 'Name'
TextInput:
id: name
Label:
text: 'Class'
TextInput:
id: clas
Button:
text: 'Ok'
Button:
text: 'Cancel'
既然您想使用 Enter 来改变焦点,我猜您不想要多行 texinput。因此,一种选择是使用 on_text_validate
事件:
on_text_validate
Fired only in multiline=False mode when the user hits ‘enter’. This will also unfocus the textinput.
在您的 kv 文件中,您可以执行如下操作:
#:kivy 1.10.0
TestScreen:
GridLayout:
cols: 2
padding : 30,30
spacing: 10, 10
row_default_height: '40dp'
Label:
text: 'Name'
TextInput:
id: name
multiline: False
on_text_validate: clas.focus = True # <<<<<<<<<<<
Label:
text: 'Class'
TextInput:
id: clas
multiline: False
Button:
text: 'Ok'
Button:
text: 'Cancel'