如何用wxPython从框架中删除动画gif?

2 投票
1 回答
688 浏览
提问于 2025-04-18 04:52

我实在搞不懂怎么从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 个回答

2

我觉得每次你调用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()

撰写回答