如何调整当前的启动画面以允许其他代码在后台运行?
目前我有一个启动画面,但它并没有真正发挥启动画面的作用——因为它会阻止其他代码的执行(而不是让它们在后台运行)。
这是我程序的当前架构(简化版),重要的部分都完整显示出来了。我该如何调整现在的启动画面,让程序的其他部分可以在后台加载呢?在Python中可以做到吗?
谢谢!
import ...
(many other imports)
def ...
def ...
(many other definitions)
class VFrams(wxFrame):
wx.Frame.__init__(self, parent, -1, _("Software"),
size=(1024, 768), style=wx.DEFAULT_FRAME_STYLE)
(a lot of code goes in here)
class MySplashScreen(wx.SplashScreen):
def __init__(self, parent=None):
aBitmap = wx.Image(name=VarFiles["img_splash"]).ConvertToBitmap()
splashStyle = wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT
splashDuration = 5000 # ms
wx.SplashScreen.__init__(self, aBitmap, splashStyle, splashDuration, parent)
self.Bind(wx.EVT_CLOSE, self.CloseSplash)
wx.Yield()
def CloseSplash(self, evt):
self.Hide()
global frame
frame = VFrame(parent=None)
app.SetTopWindow(frame)
frame.Show(True)
evt.Skip()
class MyApp(wx.App):
def OnInit(self):
MySplash = MySplashScreen()
MySplash.Show()
return True
if __name__ == '__main__':
DEBUG = viz.addText('DEBUG:', viz.SCREEN)
DEBUG.setPosition(0, 0)
DEBUG.fontSize(16)
DEBUG.color(viz.BLACK)
Start_Mainvars()
Start_Config()
Start_Translation()
Start_DB()
Start_Themes()
Start_Gui()
Start_Get_Isos()
Start_Bars()
Start_Menus()
Start_Event_Handlers()
app = MyApp()
app.MainLoop()
感谢大家的帮助,这是我根据建议修改后的代码:
def show_splash():
# create, show and return the splash screen
global splash
bitmap = wx.Image(name=VarFiles["img_splash"]).ConvertToBitmap()
splash = wx.SplashScreen(bitmap, wx.SPLASH_CENTRE_ON_SCREEN|wx.SPLASH_NO_TIMEOUT, 0, None, -1)
splash.Show()
return splash
class MyApp(wx.App):
def OnInit(self):
global frame, splash
splash = show_splash()
Start_Config()
Start_Translation()
Start_DB()
Start_Themes()
Start_Gui()
Start_Get_Isos()
Start_Bars("GDP1POP1_20091224_gdp", "1 pork")
Start_Menus()
Start_Event_Handlers()
frame = VFrame(parent=None)
frame.Show(True)
splash.Destroy()
return True
if __name__ == '__main__':
DEBUG = viz.addText('DEBUG:', viz.SCREEN)
DEBUG.setPosition(0, 0)
DEBUG.fontSize(16)
DEBUG.color(viz.BLACK)
Start_Mainvars()
app = MyApp()
app.MainLoop()
2 个回答
0
你需要用两个线程:一个线程用来显示启动画面,另一个线程用来执行你想要运行的其他代码。这两个线程可以同时运行,这样就能达到你想要的效果。
13
你的代码看起来有点乱,也挺复杂的。其实没有必要去重写 wx.SplashScreen,也没必要让你的启动画面关闭事件去创建主应用窗口。下面是我处理启动画面的方法。
import wx
def show_splash():
# create, show and return the splash screen
bitmap = wx.Bitmap('images/splash.png')
splash = wx.SplashScreen(bitmap, wx.SPLASH_CENTRE_ON_SCREEN|wx.SPLASH_NO_TIMEOUT, 0, None, -1)
splash.Show()
return splash
def main():
app = wx.PySimpleApp()
splash = show_splash()
# do processing/initialization here and create main window
frame = MyFrame(...)
frame.Show()
splash.Destroy()
app.MainLoop()
if __name__ == '__main__':
main()
你可以尽早创建启动画面,不需要设置时间限制。然后继续加载,创建你的应用主窗口。最后再关闭启动画面,这样它就消失了。显示启动画面并不会阻止其他操作的进行。