2014-08-28 98 views
2

下面的代碼應該在點擊一個按鈕時播放一些gif圖像。當點擊另一個按鈕時播放另一個gif圖像。但是當我點擊第一個按鈕時它正在播放相關的圖像正確.. 雖然通過點擊第二個按鈕,第一個圖像和第二個無限循環一個接一個地播放 ...所以如何通過按鈕點擊播放一個gif?wxpython逐個顯示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

您需要停止並銷燬之前的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() 

我加self.gif = None__init__功能和變化不大的功能animateGIF