[英]How can I access element of RecycleView Kivy?
I wonder to know how to access widgets in RecycleView.我想知道如何访问 RecycleView 中的小部件。 I constructed simple example:我构建了一个简单的例子:
main.py主文件
from kivy.app import App
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
class ShowBoxLayout(BoxLayout):
keys = ListProperty()
def __init__(self, **kwargs):
super(ShowBoxLayout, self).__init__(**kwargs)
self.keys = [x for x in range(5)]
def print_list(self):
#here I expect textinputs id but got empty dict
print(self.ids)
class TestApp(App):
def build(self):
bl = ShowBoxLayout()
return bl
app = TestApp()
app.run()
test.kv测试.kv
<ShowBoxLayout>:
RecycleView:
viewclass: 'TextInput'
data: [{'id': str(x)} for x in range(10)]
RecycleGridLayout:
cols: 1
default_size: None, dp(26)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
multiselect: False
touch_multiselect: False
Button:
text : 'Hello world'
on_press : root.print_list()
In that case I cannot access TextInput inside of it with ids nor with anything else.在这种情况下,我无法使用 id 或其他任何东西访问其中的 TextInput。 How should I access it in order to get text in them?我应该如何访问它才能在其中获取文本?
This is how screen looks like.这就是屏幕的样子。
and this is what I get after button is pressed: {}
.这就是我按下按钮后得到的: {}
。
The problem is that you cannot assign to the ids
dictionary in python.问题是您不能分配给 python 中的ids
字典。 That can only be done in kv
.这只能在kv
中完成。 So another way to access the items is to assign an id
to the RecycleGridLayout
, then visit each of its children.所以访问这些项目的另一种方法是为RecycleGridLayout
分配一个id
,然后访问它的每个子项。 You can also define a method of your viewclass
to display the entered text:您还可以定义viewclass
的方法来显示输入的文本:
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
class ShowBoxLayout(BoxLayout):
keys = ListProperty()
def __init__(self, **kwargs):
super(ShowBoxLayout, self).__init__(**kwargs)
self.keys = [x for x in range(5)]
def print_list(self):
#here I expect textinputs id but got empty dict
for child in self.ids.grid.children:
print(child, child.text, child.id)
class MyTextInput(TextInput):
def __init__(self, **kwargs):
super(MyTextInput, self).__init__(**kwargs)
self.multiline = False
self.on_text_validate = self.get_text
def get_text(self):
print('get_text:', self.text)
Builder.load_string('''
<ShowBoxLayout>:
RecycleView:
viewclass: 'MyTextInput'
data: [{'id': str(x)} for x in range(10)]
RecycleGridLayout:
id: grid
cols: 1
default_size: None, dp(26)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
multiselect: False
touch_multiselect: False
Button:
text : 'Hello world'
on_press : root.print_list()
''')
class TestApp(App):
def build(self):
bl = ShowBoxLayout()
return bl
app = TestApp()
app.run()
(I used Builder.load_string()
as a convenience for myself) (我使用Builder.load_string()
为自己提供方便)
Note that since this is a RecycleView
, the viewclass
items are recycled, so the print_list()
method may not visit an item for every data
element, but only the ones currently displayed.请注意,由于这是一个RecycleView
,因此viewclass
项目被回收,因此print_list()
方法可能不会访问每个data
元素的项目,而只会访问当前显示的项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.