wxPython中的动画飞溅
问题描述:
我在长时间拍摄功能期间正在使用wxPython和动画(gif)飞溅。到目前为止,我有:wxPython中的动画飞溅
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
任何输入可以理解
答
您应该执行的时间上的另一线程消耗的工作,否则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()
但我需要从函数中使用的返回值。基本上我希望主功能“等待”,直到其他功能完成并在返回值上进一步使用,同时进行飞溅旋转。我也尝试在单独的线程中启动splash并从main调用函数,但它只让我在主线程中运行动画。 – Magen