本文介绍了如何在带有(或不带有)ScreenManager(自动切换到下一个屏幕)的Kivy中构建启动屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python和kivy创建一个"SplashScreen".这个想法是启动程序,尽可能快地显示SplashScreen,在后台运行某些方法,然后自动切换到"MainScreen".那是用户可能输入和输入输入的第一时间.

I'm trying to create a "SplashScreen" with python and kivy. The idea is to start the program, show the SplashScreen as fast as possible, run some methods in background, then automatically switch to the "MainScreen". Thats the first moment where user input is possible and expected.

问题是,我尝试过的所有方法都得到以下结果:程序开始运行,显示空白的空白窗口,运行我的方法,显示SplashScreen,立即切换到MainScreen.

The problem is, that every way I tried, I got following result: Program starts running, shows a blank white window, runs my methods, shows the SplashScreen, immediately switches to MainScreen.

在运行我想要的方法之前,我只是不知道如何强制显示显示SplashScreen.需要澄清的是:"SplashScreen"并不意味着像透明度或东西之类的东西.只是另一个标准屏幕.

I simply don't get how to force kivy show the SplashScreen before running the methods I want. To clearify that: "SplashScreen" does not mean anthing fancy like transparency or stuff. Just another standard screen.

这是我的代码:

mainWindow.py

mainWindow.py

Builder.load_file("loadingscreen.kv")
Builder.load_file("mainscreen.kv")

class LoadingScreen(Screen):
    pass


class MainScreen(Screen):
    pass


class FaitApp(App):

    LoadingStartUp = None

    def build(self):
        self.root = ScreenManager()
        self.root.add_widget(LoadingScreen(name='LoadingScreen'))
        self.root.add_widget(MainScreen(name='MainScreen'))
        return self.root

    def on_start(self):
        self.LoadingStartUp = LoadingStartUp(self)
        Clock.schedule_once(self.LoadingStartUp.loadButtonImages,0)
        return

if __name__ == '__main__':
    app = FaitApp()
    app.run()
    sys.exit()

loadingscreen.kv

loadingscreen.kv

#kivy 1.10.1
    <LoadingScreen>:
        canvas:
            Color:
                rgba: 1, 1 , 0, 1
            Rectangle:
                size: self.size
        Label:
            font_size: 42
            text: root.name

mainscreen.kv

mainscreen.kv

#kivy 1.10.1
    <MainScreen>:
        canvas:
            Color:
                rgba: 1, 1 , 0, 1
            Rectangle:
                size: self.size
        Label:
            font_size: 42
            text: root.name

loadingStartUp.py

loadingStartUp.py

class LoadingStartUp(object):

    app = None

    def __init__(self, app):
        self.app = app
        return

    def loadButtonImages(self):
        log.debug('LoadingStartUp')
        log.debug('Waiting for everything ready!')
        sleep(5)
        self.app.root.current = 'MainScreen'
        return

我还尝试在build()on_start()中使用Clock.schedule_once().我什至尝试使用self.root = Builder.load_file()在没有ScreenManager的情况下加载网站.每次上述结果都是相同的.

I also tried to use Clock.schedule_once() within build() and on_start(). I even tried to load the sites without ScreenManager with self.root = Builder.load_file(). The result was the same every time as described above.

我认为检测出SplashScreen何时正确显示在屏幕上并在此之后运行方法就足够了.

It would be enought I think to detect when the SplashScreen is correctly shown on screen and run a method after that.

任何解决方案/提示/技巧/建议都非常感谢!谢谢!

Any solutions/hints/tipps/advices are very appreciated! Thanks!

推荐答案

我认为您可以通过从loadButtonImages()方法中删除sleep(5)并将Clock.schedule_once(self.LoadingStartUp.loadButtonImages,0)调用修改为Clock.schedule_once(self.LoadingStartUp.loadButtonImages,5)来获得所需的内容. sleep()调用可防止GUI在这5秒钟内执行任何操作.并且Clock.schedule_once()调用中的5阻止主屏幕显示5秒钟.

I think you can get what you want by eliminating the sleep(5) from the loadButtonImages() method and modify the Clock.schedule_once(self.LoadingStartUp.loadButtonImages,0) call to Clock.schedule_once(self.LoadingStartUp.loadButtonImages,5). The sleep() call prevents the GUI from doing anything during those 5 seconds. And the 5 in the Clock.schedule_once() call prevents the main screen from showing for 5 seconds.

这篇关于如何在带有(或不带有)ScreenManager(自动切换到下一个屏幕)的Kivy中构建启动屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:14