wxpython逐个显示gif图像
下面的代码是用来在点击一个按钮时播放一个gif图像,而在点击另一个按钮时播放另一个gif图像的。
但是,当我点击第一个按钮时,它会正确播放相关的图像。
而当我点击第二个按钮时,两个图像会一个接一个地无限循环播放……那么,如何才能通过按钮点击只播放一个gif呢?
import wx, wx.animate
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY)
panel = wx.Panel(self, wx.ID_ANY)
btn1 = wx.Button(self, -1, "play GIF 1",(50,10))
btn1.Bind(wx.EVT_BUTTON, self.onButton1)
btn2 = wx.Button(self, -1, "play GIF 2",(50,40))
btn2.Bind(wx.EVT_BUTTON, self.onButton2)
#----------------------------------------------------------------------
def onButton1(self, event):
image='animated_1.gif'
self.animateGIF(image)
#----------------------------------------------------------------------
def onButton2(self, event):
image='animated_2.gif'
self.animateGIF(image)
#----------------------------------------------------------------------
def animateGIF(self,image):
gif = wx.animate.GIFAnimationCtrl(self, -1, image,pos=(50,70),size=(10,10))
gif.GetPlayer()
gif.Play()
#----------------------------------------------------------------------
app = wx.App()
frame = MyForm().Show()
app.MainLoop()
1 个回答
1
在开始新的gif动画之前,你需要先停止并销毁之前的gif图像。可以这样做:
import wx, wx.animate
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY)
panel = wx.Panel(self, wx.ID_ANY)
btn1 = wx.Button(self, -1, "play GIF 1",(50,10))
btn1.Bind(wx.EVT_BUTTON, self.onButton1)
btn2 = wx.Button(self, -1, "play GIF 2",(50,40))
btn2.Bind(wx.EVT_BUTTON, self.onButton2)
self.gif = None
#----------------------------------------------------------------------
def onButton1(self, event):
image='animated_1.gif'
self.animateGIF(image)
#----------------------------------------------------------------------
def onButton2(self, event):
image='animated_2.gif'
self.animateGIF(image)
#----------------------------------------------------------------------
def animateGIF(self,image):
if self.gif:
self.gif.Stop()
self.gif.Destroy()
self.gif = wx.animate.GIFAnimationCtrl(self, -1, image,pos=(50,70),size=(10,10))
self.gif.GetPlayer()
self.gif.Play()
#----------------------------------------------------------------------
app = wx.App()
frame = MyForm().Show()
app.MainLoop()
我在__init__
函数里加了self.gif = None
,然后稍微修改了一下animateGIF
这个函数。