Skip to main content
 首页 » 编程设计

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

2025年05月04日9lonelyxmas

我这辈子都想不出如何从 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()