简体   繁体   中英

kivy button press to go to one of multiple screens

Let's say I have a kivy program where the first screen has three buttons, each of which takes you to a different screen. Each of those screens contains a button that takes you to a final screen (the same screen). The final screen has a "back" button. How do I make the "back" button on the final screen take you back to the previous screen (the 1 of 3 mentioned above)? Below is a crude diagram to better help visualize my question.

                  Screen 1
Start Screen -->  Screen 2 --> Final Screen
                  Screen 3 

As of now the "back" button on the final screen takes you back to the Start screen, but this is not what I want.

My python code:

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager

class MyScreenManager(ScreenManager):
    pass

# Branch Screen, takes you to S1, S2, or S3
class BRANCH(Screen):
    pass

class S1(Screen):
    pass

class S2(Screen):
    pass

class S3(Screen):
    pass

class FINAL_SCREEN(Screen):
    pass

class MAINApp(App):
    def build(self):
        return MyScreenManager()

if __name__ == '__main__':
    MAINApp().run()

My kivy language code:

#:kivy 1.0.9

<MyScreenManager>:
    BRANCH:
    name: 'branch'
    S1:
    name: 'screen1'
    S2:
    name: 'screen2'
    S3:
    name: 'screen3'
    FINAL_SCREEN:
    name: 'final_screen'


<BRANCH>
    FloatLayout:
    Button:
        text: 'Screen-1'
        color: 1.0, 0.6, 0.0, 1
        font_size: 30
        size_hint_x: 0.50
        size_hint_y: 0.25
        pos_hint: {'x': 0.20, 'y': 0.75}
        on_release:
            root.manager.transition.direction = 'left'
            root.manager.current = 'screen1'
    Button:
        text: 'Screen-2'
        color: 1.0, 0.6, 0.0, 1
        font_size: 30
        size_hint_x: 0.50
        size_hint_y: 0.25
        pos_hint: {'x': 0.20, 'y': 0.40}
        on_release:
            root.manager.transition.direction = 'left'
            root.manager.current = 'screen2'
    Button:
        text: 'Screen-3'
        color: 1.0, 0.6, 0.0, 1
        font_size: 30
        size_hint_x: 0.50
        size_hint_y: 0.25
        pos_hint: {'x': 0.20, 'y': 0.05}
        on_release:
            root.manager.transition.direction = 'left'
            root.manager.current = 'screen3'
<S1>
    FloatLayout:
    Label:
        pos_hint: {'x': 0.00, 'y': 0.20}
        font_size: 35
        text: 'this is SCREEN-1'
        color: 1, 1, 1, 1
    Button:
        text: 'Forward'
        color: 1.0, 0.6, 0.0, 1
        font_size: 20
        size_hint_x: 0.20
        size_hint_y: 0.20
        pos_hint: {'x': 0.40, 'y': 0.15}
        on_release:
            root.manager.transition.direction = 'left'
            root.manager.current = 'final_screen'
<S2>
    FloatLayout:
    Label:
        pos_hint: {'x': 0.00, 'y': 0.20}
        font_size: 35
        text: 'me este SCREEN-2'
        color: 1, 1, 1, 1
    Button:
        text: 'Onward'
        color: 1.0, 0.6, 0.0, 1
        font_size: 20
        size_hint_x: 0.20
        size_hint_y: 0.20
        pos_hint: {'x': 0.40, 'y': 0.15}
        on_release:
            root.manager.transition.direction = 'left'
            root.manager.current = 'final_screen'
<S3>
    FloatLayout:
    Label:
        pos_hint: {'x': 0.00, 'y': 0.20}
        font_size: 35
        text: 'something SCREEN-3'
        color: 1, 1, 1, 1
    Button:
        text: 'Lets Go'
        color: 1.0, 0.6, 0.0, 1
        font_size: 20
        size_hint_x: 0.20
        size_hint_y: 0.20
        pos_hint: {'x': 0.40, 'y': 0.15}
        on_release:
            root.manager.transition.direction = 'left'
            root.manager.current = 'final_screen'
<FINAL_SCREEN>
    FloatLayout:
    Label:
        pos_hint: {'x': 0.00, 'y': 0.20}
        font_size: 35
        text: 'Final Screen'
        color: 1, 1, 1, 1
    Button:
        text: 'Back'
        color: 1.0, 0.6, 0.0, 1
        font_size: 20
        size_hint_x: 0.20
        size_hint_y: 0.20
        pos_hint: {'x': 0.40, 'y': 0.15}
        on_release:
            root.manager.transition.direction = 'right'
            root.manager.current = 'branch'

Any help or advice is greatly appreciated.

Here is one solution using a StringProperty on the Final Screen.

I am setting it when going to the final screen

root.manager.ids.final.previous = root.name

When going back I am using it to go to this screen

root.manager.current = root.previous

This is your whole app

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder
from kivy.properties import StringProperty

kv_str = """
MyScreenManager:
    BRANCH:
        name: 'branch'
    S1:
        name: 'screen1'
    S2:
        name: 'screen2'
    S3:
        name: 'screen3'
    FINAL_SCREEN:
        id: final
        name: 'final_screen'


<BRANCH>
    FloatLayout:
    Button:
        text: 'Screen-1'
        color: 1.0, 0.6, 0.0, 1
        font_size: 30
        size_hint_x: 0.50
        size_hint_y: 0.25
        pos_hint: {'x': 0.20, 'y': 0.75}
        on_release:
            root.manager.transition.direction = 'left'
            root.manager.current = 'screen1'
    Button:
        text: 'Screen-2'
        color: 1.0, 0.6, 0.0, 1
        font_size: 30
        size_hint_x: 0.50
        size_hint_y: 0.25
        pos_hint: {'x': 0.20, 'y': 0.40}
        on_release:
            root.manager.transition.direction = 'left'
            root.manager.current = 'screen2'
    Button:
        text: 'Screen-3'
        color: 1.0, 0.6, 0.0, 1
        font_size: 30
        size_hint_x: 0.50
        size_hint_y: 0.25
        pos_hint: {'x': 0.20, 'y': 0.05}
        on_release:
            root.manager.transition.direction = 'left'
            root.manager.current = 'screen3'
<S1>:
    FloatLayout:
        Label:
            pos_hint: {'x': 0.00, 'y': 0.20}
            font_size: 35
            text: 'this is SCREEN-1'
            color: 1, 1, 1, 1
        Button:
            text: 'Forward'
            color: 1.0, 0.6, 0.0, 1
            font_size: 20
            size_hint_x: 0.20
            size_hint_y: 0.20
            pos_hint: {'x': 0.40, 'y': 0.15}
            on_release:
                root.manager.transition.direction = 'left'
                root.manager.current = 'final_screen'
                root.manager.ids.final.previous = root.name
<S2>:
    FloatLayout:
        Label:
            pos_hint: {'x': 0.00, 'y': 0.20}
            font_size: 35
            text: 'me este SCREEN-2'
            color: 1, 1, 1, 1
        Button:
            text: 'Onward'
            color: 1.0, 0.6, 0.0, 1
            font_size: 20
            size_hint_x: 0.20
            size_hint_y: 0.20
            pos_hint: {'x': 0.40, 'y': 0.15}
            on_release:
                root.manager.transition.direction = 'left'
                root.manager.current = 'final_screen'
                root.manager.ids.final.previous = root.name
<S3>:
    FloatLayout:
        Label:
            pos_hint: {'x': 0.00, 'y': 0.20}
            font_size: 35
            text: 'something SCREEN-3'
            color: 1, 1, 1, 1
        Button:
            text: 'Lets Go'
            color: 1.0, 0.6, 0.0, 1
            font_size: 20
            size_hint_x: 0.20
            size_hint_y: 0.20
            pos_hint: {'x': 0.40, 'y': 0.15}
            on_release:
                root.manager.transition.direction = 'left'
                root.manager.current = 'final_screen'
                root.manager.ids.final.previous = root.name
<FINAL_SCREEN>:
    FloatLayout:
        Label:
            pos_hint: {'x': 0.00, 'y': 0.20}
            font_size: 35
            text: 'Final Screen'
            color: 1, 1, 1, 1
        Button:
            text: 'Back'
            color: 1.0, 0.6, 0.0, 1
            font_size: 20
            size_hint_x: 0.20
            size_hint_y: 0.20
            pos_hint: {'x': 0.40, 'y': 0.15}
            on_release:
                root.manager.transition.direction = 'right'
                root.manager.current = root.previous

"""

class MyScreenManager(ScreenManager):
    pass

# Branch Screen, takes you to S1, S2, or S3
class BRANCH(Screen):
    pass

class S1(Screen):
    pass

class S2(Screen):
    pass

class S3(Screen):
    pass

class FINAL_SCREEN(Screen):
    previous = StringProperty()


class MAINApp(App):
    def build(self):
        return Builder.load_string(kv_str)

if __name__ == '__main__':
    MAINApp().run()

I tried previous() as suggested in the comments

<FINAL_SCREEN>:
    FloatLayout:
        Label:
            pos_hint: {'x': 0.00, 'y': 0.20}
            font_size: 35
            text: 'Final Screen'
            color: 1, 1, 1, 1
        Button:
            text: 'Back'
            color: 1.0, 0.6, 0.0, 1
            font_size: 20
            size_hint_x: 0.20
            size_hint_y: 0.20
            pos_hint: {'x': 0.40, 'y': 0.15}
            on_release:
                print(root.manager.previous())
                root.manager.current = root.manager.previous()

Strangely, it will always return screen3 and I therefore used the version above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM