Search code examples
pythonpython-2.7wxpython

wxpython to display gif images one by one


the code below is supposed to play some gif image when click a button..and play another gif image when clicking another button.. but when i click the first button it is playing the related image properly.. while by clicking the second button both the first image and the second one are played one by one on infinity loop ...so how to play one gif by button click?

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()

Solution

  • You need to stop and destroy the previous gif image before starting a new one. Like this:

    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()
    

    I added self.gif = None to the __init__ function and little changed the function animateGIF.