我正在试验 Kivy,并试图在 BoxLayout 中嵌套一个 ScreenManager 实例.我遇到的问题是,当 ScreenManager 是 BoxLayout 的子小部件时,ScreenManager 及其屏幕不显示.
I am experimenting with Kivy and am trying to nest a ScreenManager instance inside of a BoxLayout. The problem I am having is that the ScreenManager and its Screen do not show when the ScreenManager is a child widget of the BoxLayout.
此代码显示黑屏.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.screenmanager import Screen, ScreenManager
class MenuScreen(Screen):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
self.add_widget(Label(text="Some text."))
screen_manager = ScreenManager()
screen_manager.add_widget(MenuScreen(name="menu"))
class Container(BoxLayout):
def __init__(self, **kwargs):
super(BoxLayout, self).__init__(**kwargs)
self.add_widget(screen_manager)
class NestedScreenManagerApp(App):
def build(self):
"""
:return: a BoxLayout with the screen manager nested inside it
"""
return Container()
if __name__ == "__main__":
NestedScreenManagerApp().run()
另一方面,这段代码(它直接将 ScreenManager 作为根小部件返回)确实有效,并且 MenuScreen 及其标签是可见的.将 ScreenManager 作为根小部件返回正是 官方屏幕管理器示例应用程序 会.
On the other hand, this code (which returns the ScreenManager directly as the root widget) does work and the MenuScreen and its Label are visible. Returning the ScreenManager as the root widget is exactly what the official screen manager example app does.
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.screenmanager import Screen, ScreenManager
class MenuScreen(Screen):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
self.add_widget(Label(text="Some text."))
screen_manager = ScreenManager()
screen_manager.add_widget(MenuScreen(name="menu"))
class RootScreenManagerApp(App):
def build(self):
"""
:return: the screen manager directly
"""
return screen_manager
if __name__ == "__main__":
RootScreenManagerApp().run()
当 ScreenManager 及其 Screen 是我的 Container(BoxLayout) 的子小部件时,如何让其可见?我想我错过了一些非常简单的东西.
How can I get the ScreenManager and its Screen to be visible when it is child widget of my Container(BoxLayout)? I think I am missing something really simple.
我在 Python 2.7.9 上使用 Kivy 1.8.0,在 Debian Jessie 上运行.
I am using Kivy 1.8.0 on Python 2.7.9, running on Debian Jessie.
我找到了解决方案;问题是我在 Container.__init__
方法中调用了 super(BoxLayout, self)
而不是 super(Container, self)
.一旦我改变了这个,ScreenManager 和它的屏幕就可以在 Container 中看到了.
I found the solution; the problem was I was calling super(BoxLayout, self)
instead of super(Container, self)
in my Container.__init__
method. Once I changed this, the ScreenManager and its screen became visible from within the Container.
这篇关于嵌套在 BoxLayout 内的 ScreenManager 不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!