【问题标题】:Set default screen based on return value in Kivy根据 Kivy 中的返回值设置默认屏幕
【发布时间】:2019-05-20 17:39:04
【问题描述】:

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

我有改变屏幕的 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() 中返回的值,屏幕应该在每次运行应用程序时发生变化。

【问题讨论】:

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


    【解决方案1】:

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

    片段

    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)
    

    【讨论】:

    • 实例化,我忽略了一件简单的事情,一如既往地感谢@ikolim :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    相关资源
    最近更新 更多