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

时间:2014-04-30 12:18:50

标签: python wxpython gif

我不能为我的生活弄清楚如何从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?

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

1 个答案:

答案 0 :(得分:1)

我相信您每次调用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()