Kivy - 过渡之间的黑屏

时间:2016-09-27 12:08:49

标签: kivy

我正在尝试复制此gif,这是在Kivy中完成的(这里是link到整页)

就像我开始时一样,我注意到转换之间有一个黑屏(link到它的样子,所以你不必复制粘贴并运行)

为什么出现黑屏?

编辑:我必须在没有按钮的情况下工作。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

# Create both screens. Please note the root.manager.current: this is how
# you can control the ScreenManager from kv. Each screen has by default a
# property manager that gives you the instance of the ScreenManager used.
Builder.load_string("""
    <MenuScreen>:
    canvas.before:
        Color:
            rgba: 122,255,0,2
        Rectangle:
            pos: self.pos
            size: self.size
    Label:
        text: 'hello'


<SettingsScreen>:
    canvas.before:
        Color:
            rgba: 0,255,0,2
        Rectangle:
            pos: self.pos
            size: self.size
    Label:
        text: 'hello'
""")

# Declare both screens
class MenuScreen(Screen):
    def on_touch_down(self, touch):
        sm.current = 'settings'

class SettingsScreen(Screen):
    def on_touch_down(self, touch):
        sm.current = 'menu'

# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))

class TestApp(App):

    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

编辑:我已经尝试了这个但仍然无法正常工作

<sm>:
    canvas:
        Color:
            rgb: (0, 255, 255)
        Rectangle:
            size: self.size
            pos: self.pos

3 个答案:

答案 0 :(得分:1)

黑色区域是屏幕管理器的画布。如果你不喜欢它是黑色的,那么你可以画它,就像你用屏幕做的那样;或将转换类型更改为NoTransition以隐藏它。

另外,你应该考虑在那个kv lang字符串中构建你的屏幕管理器。

答案 1 :(得分:1)

您不应该直接使用Screen子类。相反,您必须首先添加组件(例如按钮或布局),例如使用RelativeLayout

Builder.load_string("""
<MenuScreen>:
    RelativeLayout:
        canvas.before:
            Color:
                rgba: 122,255,0,2
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            text: 'hello'


<SettingsScreen>:
    RelativeLayout:
        canvas.before:
            Color:
                rgba: 0,255,0,2
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            text: 'hello'
""")

答案 2 :(得分:0)

古老,但万一有人遇到此问题:

要弄清评论中含糊不清的答案,您需要以与绘制屏幕相同的方式来绘制屏幕管理器。

kvlang中的示例:

ScreenManagement:
    canvas.before:
        Color:
            rgba: 1,1,1,1
        Rectangle:
            size: self.size
            pos: self.pos
    id: screen_manager
    transition: NoTransition()
    Screen1:
    Screen2:
    Settings_: