如何使用wxPython从帧中删除动画gif?

如何使用wxPython从帧中删除动画gif?,python,wxpython,gif,Python,Wxpython,Gif,我一辈子都搞不懂如何从wxPython帧中销毁或隐藏gif 下面是示例代码: import wx import wx.animate ######################################################################## class MyForm(wx.Frame): #---------------------------------------------------------------------- def

我一辈子都搞不懂如何从wxPython帧中销毁或隐藏gif

下面是示例代码:

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()
那么,我该如何从帧中删除这个gif呢


谢谢大家!!我希望代码足够清晰。

我相信您每次调用animateGIF时都会加载一个新的GIF。我建议如下,但我相信这是可以改进的:

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

它确实有效!可以肯定的是:我正在创建第二个相同的gif,停止/销毁它,但第一个仍然存在?有意思,我应该看到的。好的,谢谢@WilliamAbma是的,你在旧动画的基础上创建了一个新动画并“停止”了它。与此同时,旧的动画继续在下面运行。如果执行了
销毁
,则只会销毁新动画,而不会销毁旧动画。