wxPython自定义SplashScreen与进度表

时间:2014-06-16 15:59:36

标签: python wxpython progress-bar wxwidgets splash-screen

如何在wxPython中创建一个自定义初始屏幕,其中有一个加载栏并在启动时自动开始?

我的代码示例答案如下。

1 个答案:

答案 0 :(得分:1)

我一直在尝试使用在启动时自动加载的wxPython创建一个自定义启动画面,其中包含一个进度表(加载栏)。经过一些调整,我终于能够创建一个。为了做到这一点,我不得不覆盖应用程序的MainLoop()方法。下面是我使用的应用程序模块。

class Application(wx.App):
"""Inherits from wx.App and serves as the entry point for the wxPython application. 
To run the program, an instance of this class is created and its method "MainLoop" is called.
"""

    def __init__(self):
        """Initializes our wx.App by calling its super method. It also instantiates a new 
        ApplicationManager which manages the different components of the project session.
        """
        wx.App.__init__(self, redirect = False)  


    def OnInit(self):
        """Instantiates the main frame. The OnInit method is only available for classes that derive 
        wx.App. Override this method to do application initialization.
        """
        self.appManager = ApplicationManager()
        self.appManager.applicationFrame = ApplicationFrame(self)
        self.appManager.splashScreen = SplashScreenDialog()
        self.keepRunning = True
        return True


    def MainLoop(self):
        """This function is overridden to allow the splash screen to load properly at the start 
        of the application. On the first loop, the method displays the splash screen and loads 
        the necessary files. When the application is closed, the keepRunning variable is set 
        to false (see ApplicationFrame) and the while loop is ended.
        """
        self.loadedSplashScreen = False

        # Create an event loop and make it active. If you are only going to temporarily have a nested 
        # event loop then you should get a reference to the old one and set it as the active event 
        # loop when you are done with this one...
        eventLoop = wx.GUIEventLoop()
        oldEventLoop = wx.EventLoop.GetActive()
        wx.EventLoop.SetActive(eventLoop)

        # This outer loop determines when to exit the application, for this example we let the main 
        # frame reset this flag when it closes.
        while self.keepRunning:
            # At this point in the outer loop you could do whatever you implemented your own MainLoop 
            # for. It should be quick and non-blocking, otherwise your GUI will freeze.  

            # Place splash screen events on the stack
            if (not self.loadedSplashScreen):
                self.appManager.splashScreen.Show(True)

            # This inner loop will process any GUI events until there are no more waiting.
            while eventLoop.Pending():
                eventLoop.Dispatch()

            if (not self.loadedSplashScreen):
                for i in range(0,10000000):
                    # Do loading stuff here
                    j = float(i/100000.0)
                    self.appManager.splashScreen.gauge.SetValue(j)

                self.appManager.splashScreen.Close()
                self.appManager.applicationFrame.Show(True)
                self.SetTopWindow(self.appManager.applicationFrame)
                self.loadedSplashScreen = True

            # Send idle events to idle handlers. This may need to be throttle back so 
            # there is not too much CPU time spent in the idle handlers.
            time.sleep(0.10) # Throttling back
            eventLoop.ProcessIdle()

        wx.EventLoop.SetActive(oldEventLoop)

关闭应用程序时,需要将self.keepRunning设置为false才能完成MainLoop()方法。希望这有帮助!