[英]Kivy: How to change ScreenManager's "current" property on __init__
My App is a ScreenManager.我的应用程序是一个 ScreenManager。 Depending on the circumstances, I want the active screen to either be "Screen 1" or "Screen 2" when the app opens.根据具体情况,我希望应用程序打开时活动屏幕为“屏幕 1”或“屏幕 2”。 How would I do this the most elegant way?我将如何以最优雅的方式做到这一点? I thought that this is as trivial as changing the current
property in the initialization of the app.我认为这与在应用程序初始化时更改current
属性一样微不足道。 Sadly, this does not work.可悲的是,这不起作用。 Here's what should work imo:在我看来,这是应该起作用的:
main.py:主要文件:
MyApp(App):
def build(self):
return Builder.load_file("MyApp.kv")
def __init__(self, **kwargs):
super(MyApp, self).__init__(**kwargs)
if foo: # Here's the problem:
self.root.current = "Screen 1"
else:
self.root.current = "Screen 2"
MyApp.kv:我的应用程序.kv:
ScreenManager:
Screen1:
name: "Screen 1"
Screen2:
name: "Screen 2"
<Screen1@Screen>
etc...
But it doesn't.但事实并非如此。 Throws the following error:抛出以下错误:
self.root.current = "Screen 1"
AttributeError: 'NoneType' object has no attribute 'current'
My guess is that I set the current
attribute to early, before root
is set.我的猜测是我在设置root
之前将current
属性设置为 early。 An idea of mine is to 1) create a property-var for MyApp, 2) set current
to be that property, 3) change that property in the init method.我的想法是 1) 为 MyApp 创建一个 property-var,2) 将current
设置为该属性,3) 在 init 方法中更改该属性。 That's a lot of effort and code-cluttering just to change a screen on initialization.仅仅为了在初始化时更改屏幕就需要大量的工作和代码混乱。
How would I do it?我该怎么做? Thanks a lot in advance!非常感谢!
That's simply because you don't have self.root
object specified. 这仅仅是因为您没有指定self.root
对象。 Why would you need to change Screens during __init__
? 为什么在__init__
期间需要更改屏幕? You should use build
function for that. 您应该为此使用build
功能。
My example: 我的例子:
import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
Builder.load_string('''
<Root>:
Screen:
name: "Screen 1"
Label:
text: "Screen 1!"
Screen:
name:"Screen 2"
Label:
text: "Screen 2!"
''')
class Root(ScreenManager):
pass
class MyApp(App):
def build(self):
self.root = Root()
foo = random.randint(0,1)
if foo:
self.root.current = "Screen 1"
else:
self.root.current = "Screen 2"
return self.root
MyApp().run()
self.root.cureent_screen
property will be changed before self.root
object will be visible 在self.root
对象可见之前,将更改self.root.cureent_screen
属性
Only this is working.只有这个有效。 I said above statement because I tryed many codes(but not works)我说上面的陈述是因为我尝试了很多代码(但没有用)
class NextScreen(ScreenManager): class 下一个屏幕(屏幕管理器):
def __init__(self,**kwargs):
super(NextScreen,self).__init__(**kwargs)
#code goes here and add:
Window.bind(on_keyboard=self.Android_back_click)
def Android_back_click(self,window,key,*largs):
# print(key)
if key == 27:
if self.current_screen.name == "Screen1" or self.current_screen.name == "Screen_main":
return False
elif self.current_screen.name == "Screen2":
try:
self.current = "Screen1"
except:
self.current = "Screen_main"
return True
Use 'esc' button to get key(27) which means back in android使用'esc'按钮获取密钥(27),这意味着回到android
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.