ScreenManager nested inside a BoxLayout is not visible(嵌套在 BoxLayout 内的 ScreenManager 不可见)
问题描述
我正在试验 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 不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:嵌套在 BoxLayout 内的 ScreenManager 不可见
基础教程推荐
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
