Pedro Couto
Pedro Couto

Reputation: 21

Kivy HELP: Widget and Screen manager

So I'm creating an app, and i need to have this thing:

Widget 1: - GridLayout with data from a JSON file, each data row goes to a button, so basically when you click on the button a popup box shows up. - Popup: This one contains a numeric keyboard to input a password, and then you click on a button to enter on the main widget

Main Widget: - This one reads data from a JSON file and then puts it on the grid layout, just like on the widget 1

I can do the widgets just fine, in python, not in kv language, and I just cant do a thing: change from widget 1 to main widget... Please help me, i'm stuck with this thing for a long long time...

Upvotes: 2

Views: 3118

Answers (1)

toto_tico
toto_tico

Reputation: 19027

In order to change between screens you just need to use the current property. Basically you have to tell the ScreenManager which is the current screen but first you have to put a a name on them. Here you have an example:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

Builder.load_string("""
<Phone>:
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                Label: 
                    text: 'The first screen'
            Screen:
                name: 'screen2'
                Label: 
                    text: 'The second screen'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .1
            Button:
                text: 'Go to Screen 1'
                on_press: _screen_manager.current = 'screen1'
            Button:
                text: 'Go to Screen 2'
                on_press: _screen_manager.current = 'screen2'""")

class Phone(FloatLayout):
    pass

class TestApp(App):
    def build(self):
        return Phone()

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

The line

on_press: _screen_manager.current = 'screen1'

will tell the screen manager to change the screen named 'screen1' with this other line

name: 'screen1'

Upvotes: 3

Related Questions