Mate
Mate

Reputation: 301

Kivy Screen Manager Can't Switch Between Screens

I want to switch to the second screen and back, but when i hit the button which I had bind to it, does nothing.

I looked around online there are multiple ways to switch between screens, but I found this way the easiest and cleanest, though it doesn't work for me.

The code will be self-explanatory.

python code:

class Manager(ScreenManager):
    pass


class Screen_one(Screen):
    pass


class Screen_two(Screen):
    pass


class Screen_three(Screen):
    pass

a part of the kv file:

    ScreenManager:
        id: sm
        Screen_one:
            id: screen_one
            name: 'screen1'
            manager: 'sm'
        Screen_two:
            id: screen_two
            name: 'screen2'
            manager: 'sm'
        Screen_three:
            id: screen_three
            name: 'screen3'
            manager: 'sm'
<Screen_one>:
    FloatLayout:
        Button:
            text: "Click1"
            size_hint: .2, .05
            pos_hint: {'x': .2, 'y': .4}
            on_release: app.root.current = 'screen2'
        Label:
            text: 'Hello!'
            pos_hint: {'x': -0.2, 'y': 0}

<Screen_two>:
    FloatLayout:
        canvas.before:
            Color:
                rgba: 1, 0, 0, 1
            Rectangle:
                size: self.size
                pos: self.pos
        Button:
            text: "Click2"
            size_hint: .2, .05
            pos_hint: {'x': .1, 'y': .3}
            on_release: app.root.current = 'screen1'

Upvotes: 0

Views: 492

Answers (1)

ikolim
ikolim

Reputation: 16011

Replace all occurrence of app.root.current with app.root.ids.sm.current because root is not a ScreenManager, and use ids and sm to reference the child, ScreenManager:.

Kv Language » ids

When your kv file is parsed, kivy collects all the widgets tagged with id’s and places them in this self.ids dictionary type property.

Upvotes: 1

Related Questions