我正在使用在不同屏幕上包含多个阶段的应用程序,因此我选择RecycleView作为阶段屏幕。现在,我只想通过单击具有viewclass Button的RecycleView的不同按钮来到达不同的屏幕。
我提供的代码不是实际的代码,但这是具有所需屏幕的部分。只需单击带有文本“ 1”的RV按钮来帮助我访问屏幕(名称:“ first”),剩下的我自己动手做。
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.recycleview import RecycleView
Builder.load_string('''
<ExampleRV>:
viewclass: 'Button'
RecycleBoxLayout:
size_hint_y: None
size_hint_x: None
pos_hint: {'x': 0,'top': 0}
height: self.minimum_height
width: self.minimum_width
orientation: 'horizontal'
<Manager>:
Screen:
name: 'main'
Button:
text: "Press me"
size_hint: 0.8, 0.2
pos_hint: {"x":0.1, "y":0.1}
on_release: root.current = 'next'
Screen:
name: 'next'
ExampleRV:
Screen:
name: 'first'
Button:
text:'Press to go back'
on_release:
root.current= 'main'
''')
class ExampleRV(RecycleView):
def __init__(self, **kwargs):
super(ExampleRV, self).__init__(**kwargs)
self.data = [{'text': str(x)} for x in range(1,21)]
class Manager(ScreenManager):
pass
sm = Manager()
class myApp(App):
def build(self):
return sm
if __name__ == '__main__':
myApp().run()
答案 0 :(得分:0)
必须创建具有ScreenManager作为其属性的自定义按钮,并在更改按钮和更改屏幕时按下的屏幕名称。该信息必须通过data属性发送。考虑到上述情况,我已经修改了您的代码生成2屏幕:第一个和第二个与2个按钮相关联:
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string(
"""
<ManagerButton@Button>:
manager: None
name_screen: ""
on_press:
self.manager.current = self.name_screen
<ExampleRV>:
viewclass: 'ManagerButton'
RecycleBoxLayout:
size_hint_y: None
size_hint_x: None
pos_hint: {'x': 0,'top': 0}
height: self.minimum_height
width: self.minimum_width
orientation: 'horizontal'
<Manager>:
Screen:
name: 'main'
Button:
text: "Press me"
size_hint: 0.8, 0.2
pos_hint: {"x":0.1, "y":0.1}
on_release: root.current = 'next'
Screen:
name: 'next'
ExampleRV:
data: [ {"text": str(i), "manager": root, "name_screen": name} for i, name in enumerate(self.screen_names, 1) ]
Screen:
name: 'first'
Button:
text:'First: Press to go back'
on_release:
root.current= 'main'
Screen:
name: 'second'
Button:
text:'Second: Press to go back'
on_release:
root.current= 'main'
"""
)
class ExampleRV(RecycleView):
screen_names = ListProperty(["first", "second"])
class Manager(ScreenManager):
pass
sm = Manager()
class myApp(App):
def build(self):
return sm
if __name__ == "__main__":
myApp().run()