最近我和Kivy的Screenmanager有些问题。从switch_to
类访问管理器时,我无法使用管理器的Screen
方法。在Screen
班级中:self.manager.switch_to
会出错:AttributeError: 'NoneType' object has no attribute 'switch_to'
。我有点绝望。下面产生错误的简单应用程序。
import kivy
kivy.require("1.9.1")
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
screen1 = True
class Screen1(Screen):
def __init__(self, **kw):
super(Screen1, self).__init__(**kw)
self.add_widget(Label(text="Screen1"))
class Screen2(Screen):
def __init__(self, **kw):
super(Screen2, self).__init__(**kw)
self.add_widget(Label(text="Screen2"))
class BlackMenu(Screen):
def __init__(self, **kw):
super(BlackMenu, self).__init__(**kw)
if screen1:
self.manager.switch_to(Screen1())
class MyApp(App):
def build(self):
mymanager = ScreenManager()
mymanager.switch_to(BlackMenu(name="black"))
return mymanager
if __name__ == "__main__":
MyApp().run()
答案 0 :(得分:0)
尝试将屏幕管理器声明为screen1
下的全局变量import kivy
kivy.require("1.9.1")
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
screen1 = True
screen_manager = ScreenManager()
然后在您的构建功能中添加您的屏幕,如:
def build(self):
screen_manager.add_widget(Screen1(name='screen1'))
screen_manager.add_widget(Screen2(name='screen2'))
screen_manager.add_widget(BlackMenu(name='black_menu'))
return screen_manager
然后在黑屏中你可以做到:
class BlackMenu(Screen):
def __init__(self, **kw):
super(BlackMenu, self).__init__(**kw)
if screen_manager.current == 'screen1':
screen_manager.switch_to(Screen1())
但请记住,切换到屏幕会从screen_manager子项中删除该屏幕,因此您必须再次管理添加它。 祝你好运