Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change screen transition in different screens

Tags:

python

kivy

I have two screen. When I press the 'next screen' button it will go to the 2nd screen and the transition should be SlideTransition (left) and when I press the 'back' button it should go back to the 1st screen and the transition should be SlideTransition (right).

How to do it? it only uses 1 transition in my code

This is my code py/kv code:

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()
like image 318
KPA Avatar asked Feb 26 '16 04:02

KPA


People also ask

How do you alternate between different screens?

To switch displays, hold down the left CTRL key + left Windows Key, and use the left and right arrow keys to cycle through the available displays.


2 Answers

Put the transitions in on_release events.

Button:
    text: 'next screen'
    on_release:
        app.root.transition = SlideTransition(direction='right')
        app.root.current = '2ndScreen'
...
like image 200
jligeza Avatar answered Sep 30 '22 09:09

jligeza


For anyone else coming here looking for the answer to use entirely different transitions from one screen to another, this is what worked for me:

  1. Load the desired transitions at the top of the KV file with lines like this (hash included):
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import SlideTransition kivy.uix.screenmanager.SlideTransition
  1. Play around with this format changing the transition type to the desired type:
app.root.transition = FadeTransition(duration=1.2)

Keep it linked with your triggering event. I tried

on_release

for my login and sign off buttons but found that it was glitchy and not exactly the behavior I was going for. I ended up adding a line for

on_press

in order to create the desired transitions without the unexpected behavior. I kept the actual line that changes the screen under the

on_release

line.

like image 21
Jordon Gonzales Avatar answered Sep 30 '22 09:09

Jordon Gonzales