Search code examples
pythonpython-3.xkivykivy-language

Set default screen based on return value in Kivy


My App has three screens, on running the App the first screen is always the same screen, on running the app, I need it to display different screens based on the return values of a specific function.

I have the UI to change screens and a function that returns a numerical value, based on this logic the screens should change.

main.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.core.window import Window


class ScreenMain(Screen):
    pass


class ScreenOne(Screen):
    pass


class ScreenTwo(Screen):
    pass


class Manager(ScreenManager):
    screen_main_id = ObjectProperty()
    screen_one_id = ObjectProperty()
    screen_two_id = ObjectProperty()


class ScreenmainApp(App):

    def build(self):
    '''This method returns the Manager class'''
        self.auth()
        return Manager()

    def auth(self):
    '''This function is called by build(), return
    value should determine which screen is displayed on running the App,
    by default the MAIN SCREEN IS FIRST SHOWN'''

        a = 3
        b = 5
        value = a + b
        if value >0 <= 5:
            print('Show screen 1')
        elif value >5<=10:
            print('Show screen 2')
        else:
            print('Show main screen')
        print('This is the return value: ',value)

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

screenmain.kv

#:kivy 1.10.0
#:include screenone.kv
#:include screentwo.kv

<Manager>:
    screen_main: screen_main_id
    screen_one: screen_one_id
    screen_two: screen_two_id

# The order below determines which screen is displayed after app loads
    ScreenMain:
        id: screen_main_id
        name: 'ScreenMain'

    ScreenOne:
        id: screen_one_id
        name: 'Screen1'

    ScreenTwo:
        id: screen_two_id
        name: 'Screen2'

<ScreenMain>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text:"WELCOME TO THE MAIN SCREEN"
        Button:
            text:'Go to Screen 1'
            on_press: root.manager.current = 'Screen1'
        Button:
            text:'Go to Screen 2'
            on_press: root.manager.current = 'Screen2'
        Label:

screenone.kv

#:kivy 1.10.0

<ScreenOne>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'This is SCREEN ONE'
        BoxLayout:
            orientation: 'horizontal'
            Button:
                text: "Go to Screen 2"
                on_press: root.manager.current = 'Screen2'
            Button:
                text: "Back to Home"
                on_press: root.manager.current = 'ScreenMain'

screentwo.kv

#:kivy 1.10.0

<ScreenTwo>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'This is SCREEN TWO'
        BoxLayout:
            orientation: 'horizontal'            
            Button:
                text: "Go to Screen 1"
                on_press: root.manager.current = 'Screen1'
            Button:
                text: "Go to Home"
                on_press: root.manager.current = 'ScreenMain'

Actual results: On App load up, the main screen always shows up first. Expected results: Based on the value returned in auth() the screens are supposed to change each time on running the app.


Solution

  • The solution is instantiate the ScreenManager before calling self.auth() method.

    Snippets

    def build(self):
    
        '''This method returns the Manager class'''
        self.root = Manager()
        self.auth()
        return self.root
    
    def auth(self):
    
        '''This function is called by build(), return
        value should determine which screen is displayed on running the App,
        by default the MAIN SCREEN IS FIRST SHOWN'''
    
        a = 3
        b = 5
        value = a + b
        if value > 0 <= 5:
            print('Show screen 1')
            self.root.current = 'Screen1'
        elif value > 5 <= 10:
            print('Show screen 2')
            self.root.current = 'Screen2'
        else:
            print('Show main screen')
            self.root.current = 'ScreenMain'
        print('This is the return value: ', value)