我在kivy的python中有一个关于动态listview的问题。所以listview正在运行,但我知道我想从点击的listview项中获取文本,将它们推送到python文件。但我无法找到适合我的任何解决方案。我认为一种方法是使用" on_selection_change"但是我无法得到这个。 所以这是我的代码
BoxLayout:
pos: 130,10
size: 500,400
ListView:
adapter:
ListAdapter(data=root.my_data,
args_converter=lambda row_index, an_obj: {'text': an_obj,'size_hint_y': 28,'height': 40, 'font_size': 20},
selection_mode='single',
allow_empty_selection=True,
cls=ListItemButton)
<ListItemButton>:
#on_selection_change=self.on_selection_change
on_release: app.getname()
和我的python文件
class PCScreen(GridLayout,Screen):
filename = open("/home/pi/Documents/ppcont/config/name.txt")
my_data = ListProperty(filename)
def getname(self):
pass
答案 0 :(得分:0)
我真的认为你应该转储ListView
,它已被弃用(来自文档):
ListAdapter:
Module: kivy.adapters.listadapter Added in 1.5
Deprecated since version 1.10.0: The feature has been deprecated.
答案 1 :(得分:0)
对于ListView,使用 ModalView 来控制选择并调用 getname()方法。有关详细信息,请参阅两个示例(ListView和RecycleView)。
ListView已弃用,请改用RecycleView。
from kivy.app import App
from kivy.uix.modalview import ModalView
from kivy.properties import ObjectProperty
class MyListView(ModalView):
list_view = ObjectProperty(None)
def __init__(self, **kwargs):
super(MyListView, self).__init__(**kwargs)
self.list_view.adapter.bind(on_selection_change=self.callback_function)
def callback_function(self, instance):
print(instance)
App.get_running_app().getname()
class MainListViewApp(App):
title = "ListView ListItemButton Demo"
def build(self):
return MyListView()
def getname(self):
print("App.getname() - Called")
if __name__ == '__main__':
MainListViewApp().run()
#:kivy 1.10.0
#:import lv kivy.uix.listview
#:import la kivy.adapters.listadapter
<MyListView>:
list_view: list_view
ListView:
id: list_view
adapter:
la.ListAdapter(
data=["Item #{0}".format(i) for i in range(100)],
selection_mode='single',
allow_empty_selection=True,
cls=lv.ListItemButton)
from kivy.app import App
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.properties import ObjectProperty
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
""" Adds selection and focus behaviour to the view. """
pass
class SelectableButton(RecycleDataViewBehavior, Button):
""" Add selection support to the Label """
index = None
def refresh_view_attrs(self, rv, index, data):
""" Catch and handle the view changes """
self.index = index
return super(SelectableButton, self).refresh_view_attrs(rv, index, data)
def on_release(self):
print("Pressed & released on", self.text)
App.get_running_app().getname()
class RV(RecycleView):
rv_layout = ObjectProperty(None)
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data = [{'text': "Button " + str(x), 'id': str(x)} for x in range(100)]
class MainRecycleViewApp(App):
title = "RecycleView Button Demo"
def build(self):
return RV()
def getname(self):
print("RecycleView: App.getname() - Called")
if __name__ == "__main__":
MainRecycleViewApp().run()
#:kivy 1.10.0
<SelectableButton>:
# Draw a background to indicate selection
canvas.before:
Color:
rgba: (0.0, 0.9, 0.1, 0.3)
Rectangle:
pos: self.pos
size: self.size
<RV>:
rv_layout: layout
viewclass: 'SelectableButton'
SelectableRecycleBoxLayout:
id: layout
default_size: None, dp(26)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: "vertical"