Skip to main content
 首页 » 编程设计

python之带有进度表的 wxPython 自定义 SplashScreen

2025年05月04日11me-sa

如何在 wxPython 中创建一个带有加载栏并在启动时自动开始的自定义启动画面?

我的答案代码示例如下。

请您参考如下方法:

我一直在尝试创建一个自定义启动画面,其中包含一个进度条(加载条),使用 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() 方法完成。希望这对您有所帮助!