Search code examples
pythonwxpythongif

How to delete an animated gif from a frame with wxPython?


I can't for the life of me figure out how to destroy or hide a gif from a wxPython frame.

Here is the example code:

import wx
import wx.animate


########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "GIF frame")

        panel = wx.Panel(self, wx.ID_ANY)
        btn1 = wx.Button(self, -1, "Start GIF",(50,10))
        btn1.Bind(wx.EVT_BUTTON, self.onButton1)

        btn2 = wx.Button(self, -1, "Stop GIF",(50,40))
        btn2.Bind(wx.EVT_BUTTON, self.onButton2)

    #----------------------------------------------------------------------
    def onButton1(self, event):
        self.animateGIF()

    #----------------------------------------------------------------------
    def onButton2(self, event):
        self.animateGIF(False)

    #----------------------------------------------------------------------
    def animateGIF(self, state=True):
        gif_fname = "test.gif"
        gif = wx.animate.GIFAnimationCtrl(self, -1, gif_fname,pos=(50,70),size=(10,10))
        gif.GetPlayer()
        if state:
            gif.Play()
        else:
            gif.Stop()
            #gif.Destroy(), gif.Hide() have no effect besides cancelling the Stop() function.


#----------------------------------------------------------------------
# Run the program
app = wx.App()
frame = MyForm().Show()
app.MainLoop()

So, how do I go about deleting this gif from my frame ?

Thank you! I hope the code is clear enough.


Solution

  • I believe you are loading a new GIF every time you call animateGIF. I suggest the following, though I am sure this can be improved:

    import wx
    import wx.animate
    
    ########################################################################
    class MyForm(wx.Frame):
    
        #----------------------------------------------------------------------
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "GIF frame")
    
            # panel not used in this example
            #panel = wx.Panel(self, wx.ID_ANY)
            self.btn1 = wx.Button(self, -1, "Start GIF",(50,10))
            self.btn1.Bind(wx.EVT_BUTTON, self.onButton1)
    
            self.btn2 = wx.Button(self, -1, "Stop GIF",(50,40))
            self.btn2.Bind(wx.EVT_BUTTON, self.onButton2)
    
            self.gif = None
    
        #----------------------------------------------------------------------
        def onButton1(self, event):
            self.animateGIF()
    
        #----------------------------------------------------------------------
        def onButton2(self, event):
            self.animateGIF(False)
    
        #----------------------------------------------------------------------
        def animateGIF(self, state=True):
            if self.gif == None:
                gif_fname = "test.gif"
                self.gif = wx.animate.GIFAnimationCtrl(self, -1, gif_fname,pos=(50,70),size=(10,10))
            # call to GetPlayer was unnecessary
            #gif.GetPlayer()
            if state:
                self.gif.Play()
            else:
                self.gif.Stop()
                self.gif.Destroy()
                self.gif = None
    
    #----------------------------------------------------------------------
    # Run the program
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()