用箭头改变焦点并访问 kivy 中的子项
根据 kivy 在组件之间移动的情况,我有两个问题。
.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
Builder.load_file("keyboardreader.kv")
class MyTextInput(TextInput):
focused=StringProperty('id1')
def keyboard_on_key_down(self, window, keycode, text, modifiers):
print(keycode, text, modifiers)
if keycode[1] == "backspace":
print("print backspace down", keycode)
TextInput.keyboard_on_key_down(self, window, keycode, text, modifiers)
if keycode[1]=="right":
if int(self.focused[2])<5:
focusedid=int(self.focused[2])+1
self.focused="id"+str(focusedid)
if keycode[1]=="left":
if int(self.focused[2])>1:
focusedid=int(self.focused[2])-1
self.focused="id"+str(focusedid)
print(self.focused)
class MainScreen(Widget):
pass
class TestingappApp(App):
def build(self):
return MainScreen()
TestingappApp().run()
.kv
<MainScreen>:
BoxLayout:
id: layout
size: root.width,root.height
MyTextInput:
id: id1
MyTextInput:
id: id2
MyTextInput:
id: id3
MyTextInput:
id: id4
MyTextInput:
id: id5
根据这段代码我几乎没有问题。
- 如何从 .kv 文件中访问具有特定 id 的元素? 我一直使用相同的表格,以前可以用,但现在不行了。例如,如果我想关注 id1 的元素,我正在考虑使用“
self.ids["id1"].focus=True
- 如何访问元素的所有子元素?”例如,我想要 id:layout 的 BoxLayout 的子级列表,其中有 id1,id2,...,id5 元素。
I have two questions according to kivy moving between components.
.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
Builder.load_file("keyboardreader.kv")
class MyTextInput(TextInput):
focused=StringProperty('id1')
def keyboard_on_key_down(self, window, keycode, text, modifiers):
print(keycode, text, modifiers)
if keycode[1] == "backspace":
print("print backspace down", keycode)
TextInput.keyboard_on_key_down(self, window, keycode, text, modifiers)
if keycode[1]=="right":
if int(self.focused[2])<5:
focusedid=int(self.focused[2])+1
self.focused="id"+str(focusedid)
if keycode[1]=="left":
if int(self.focused[2])>1:
focusedid=int(self.focused[2])-1
self.focused="id"+str(focusedid)
print(self.focused)
class MainScreen(Widget):
pass
class TestingappApp(App):
def build(self):
return MainScreen()
TestingappApp().run()
.kv
<MainScreen>:
BoxLayout:
id: layout
size: root.width,root.height
MyTextInput:
id: id1
MyTextInput:
id: id2
MyTextInput:
id: id3
MyTextInput:
id: id4
MyTextInput:
id: id5
I have few problems according to this code.
- How can I access the element with certain id from the .kv file?
I was always using the same form and it used to work, now it doesn't. For example, if I wanted to make a focus on element with id1, I was thinking about using
self.ids["id1"].focus=True
- How can I access all children elements of an element? For example, I want the list of children of BoxLayout with id:layout, that there are id1,id2,...,id5 elements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常为了访问某个小部件的子部件,您可以使用 prop.该小部件的
子级
。但从以下...看来您想将所有子项打包到某个东西中(这里,在'BoxLayout with id:layout'中)。为此,我将
kvlang
中的代码修改为这样,这将使您能够通过
ids
访问其子项(即TextInput
对象)。另请注意,您必须使用不同的focused
属性初始化每个MyTextInput
实例(以便稍后访问它们)。现在保持方法
keyboard_on_key_down
不变,我将focused
属性绑定到回调函数,例如change_focus
(您可以使用on_
prop 方法或采取任何其他方法),注意:
这应该使方法
keyboard_on_key_down
按预期发生,但您再次需要更改其中的一些逻辑才能获得完美的结果。Usually in order to access the children of a certain widget you can use the prop.
children
of that widget. But from the following...it seems you want to pack all the children into something (here, in 'BoxLayout with id:layout'). For this I modified your code in
kvlang
into this,This will enable you to access its children (i.e.
TextInput
objects) byids
. Also note that you have to initialize eachMyTextInput
instance with differentfocused
property (in order to access them later).Now keeping the method
keyboard_on_key_down
as it is, I bind thefocused
property to a callback function, say,change_focus
(you can useon_
prop method or take any other approaches) as,Note:
This should make the method
keyboard_on_key_down
happen as expected but again you need to change some logic in it to get perfect result.