wxPython中的动画启动画面

时间:2016-05-15 19:33:04

标签: python wxpython animated-gif splash

在长时间拍摄功能中,我正在努力使用wxPython和动画(gif)启动画面。到目前为止,我有:

class Splash(wx.SplashScreen):

    def __init__(self, parent=None, id=-1):

        image = "spinner.gif"
        aBitmap = wx.Image(name =image).ConvertToBitmap()
        splashStyle = wx.SPLASH_CENTRE_ON_PARENT
        splashDuration = 0 # milliseconds
        wx.SplashScreen.__init__(self, aBitmap, splashStyle,
                                 splashDuration, parent)

        gif = wx.animate.GIFAnimationCtrl(self, id, image,)

        self.Show()
        self.gif = gif

    def Run(self,):
        self.gif.Play()

我想做点什么:

splash = Splash()
splash.Run()
result = very_time_consuming_function()
splash.Close()
...
use the result

任何输入都将受到赞赏

1 个答案:

答案 0 :(得分:1)

您应该在另一个线程上执行耗时的工作,否则GUI将阻止而不响应。

  • 让工作线程执行耗时的任务。
  • 完成任务后,通知GUI线程,以便破坏启动。

这是一个片段:

import wx
import wx.animate
from threading import Thread
import time

def wrap_very_time_consuming_function():
    print "sleeping"
    time.sleep(5) # very time consuming function
    print "waking up"
    wx.CallAfter(splash.gif.Stop)
    return 0

app = wx.App()
splash = Splash()
splash.Run()
Thread(target=wrap_very_time_consuming_function).start()
app.MainLoop()