根据 Kivy 中的 return 值设置默认屏幕
Set default screen based on return value in Kivy
我的应用程序有三个屏幕,在 运行 上,应用程序的第一个屏幕始终是同一屏幕,在 运行 应用程序上,我需要它根据 [=33] 显示不同的屏幕=] 特定函数的值。
我有 UI 来改变屏幕和 return 一个数值的功能,根据这个逻辑屏幕应该改变。
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() 中编辑的值 return,屏幕应该每次在 运行 应用程序上更改。
解决方案是在调用 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)
我的应用程序有三个屏幕,在 运行 上,应用程序的第一个屏幕始终是同一屏幕,在 运行 应用程序上,我需要它根据 [=33] 显示不同的屏幕=] 特定函数的值。
我有 UI 来改变屏幕和 return 一个数值的功能,根据这个逻辑屏幕应该改变。
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() 中编辑的值 return,屏幕应该每次在 运行 应用程序上更改。
解决方案是在调用 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)