根据Kivy中的返回值设置默认屏幕

时间:2019-05-20 17:39:05

标签: python python-3.x kivy kivy-language

我的应用程序具有三个屏幕,在运行该应用程序时,第一个屏幕始终是同一屏幕,在运行该应用程序时,我需要它根据特定函数的返回值显示不同的屏幕。

我具有用于更改屏幕的UI和返回数值的函数,基于此逻辑,屏幕应该进行更改。

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'

实际结果:在加载应用程序时,始终始终首先显示主屏幕。 预期结果:根据auth()中返回的值,屏幕在每次运行应用程序时都会改变。

1 个答案:

答案 0 :(得分:1)

该解决方案是在调用ScreenManager方法之前实例化self.auth()

摘要

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)