嵌套在BoxLayout中的ScreenManager不可见 [英] ScreenManager nested inside a BoxLayout is not visible

查看:38
本文介绍了嵌套在BoxLayout中的ScreenManager不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试Kivy,并试图将ScreenManager实例嵌套在BoxLayout中.我遇到的问题是,当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()

当它是Container(BoxLayout)的子窗口小部件时,如何使ScreenManager及其屏幕可见?我想我缺少了一些非常简单的东西.

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.

我正在Debian Jessie上运行的Python 2.7.9上使用Kivy 1.8.0.

I am using Kivy 1.8.0 on Python 2.7.9, running on Debian Jessie.

推荐答案

我找到了解决方案;问题是我在Container.__init__方法中调用的是super(BoxLayout, self)而不是super(Container, self).更改此设置后,可以从Container中看到ScreenManager及其屏幕.

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不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆