我在处理一个问题,无法找到在按下ListView项目时通过过程传递值的方法。下面的示例是我想要发生的事情以及我的主要代码如何设置的简化版本。
目标是在KV文件中看到带有哈希标记的on_release:上的ChangeScreen(self.index)。然而问题是我不知道如何成功调用ChangeScreen。谢谢!
Kivy:
#: import main main
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
AppScreenManager:
DemoScreen1:
id: screen1
DemoScreen2:
id: screen2
<DemoScreen1>:
name: "demoscreen1"
BoxLayout:
orientation: "vertical"
ListView:
adapter:
ListAdapter(data=["Screen 1","Screen 2"], cls=main.ListButton)
<DemoScreen2>:
name: "demoscreen2"
orientation: "vertical"
BoxLayout:
orientation: "vertical"
Label:
id: labText
text: "Hello World"
<ListButton>:
height: self.texture_size[1]
on_release: ### HOW DO I CALL THE ChangeScreen FUNCTION HERE
Python:
import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty, ObjectProperty, ListProperty
from kivy.uix.listview import ListItemButton
class DemoScreen1(Screen):
def ChangeScreen(self, option):
if option == 0:
print("Screen1")
self.parent.ids.screen2.labelUpdater("Screen 1 was pressed")
else:
print("Screen2")
self.parent.ids.screen2.labelUpdater("Screen 2 was pressed")
self.parent.current = "demoscreen2"
class DemoScreen2(Screen):
labText = StringProperty()
def labelUpdater(self,newText):
self.ids.labText.text = newText
class ListButton(ListItemButton):
data = ListProperty()
class AppScreenManager(ScreenManager):
pass
class Tester(App):
pass
if __name__ == '__main__':
Tester().run()