我有两个屏幕。当我按下“下一个屏幕”按钮时,它会转到第二个屏幕,过渡应该是 SlideTransition(左),当我按下“后退”按钮时,它应该回到第一个屏幕,过渡应该是 SlideTransition(右) )。

怎么做?它只在我的代码中使用 1 个转换

这是我的代码 py/kv 代码:

class 1stScreen(Screen):
    ...some methods...

class 2ndScreen(Screen):
    ...some methods...

class MyScreenManager(ScreenManager):
    pass

root_widget = Builder.load_string('''
#:import SlideTransition kivy.uix.screenmanager.SlideTransition

MyScreenManager:
    transition: SlideTransition(direction='left')
    1stScreen:

    transition: SlideTransition(direction='right') #<-- Im getting error with this
    2ndScreen:

<1stScreen>:
     name: '1stScreen'
     ...some widgets...
     Button:
         text: 'next screen'
         on_release: app.root.current = '2ndScreen'

<2ndScreen>:
     name: '2ndScreen'
     ...some widgets...
     Button:
         text: 'back'
         on_release: app.root.current = '1stScreen'
''')

class Main(App):
    def build(self):
        return root_widget

if __name__ == "__main__":
    Main().run()

最佳答案

将转换放在 on_release 事件中。

Button:
    text: 'next screen'
    on_release:
        app.root.transition = SlideTransition(direction='right')
        app.root.current = '2ndScreen'
...

10-04 20:44