python - wxpython 一张一张显示gif图片

标签 python python-2.7 wxpython

下面的代码应该在单击一个按钮时播放一些 gif 图像..并在单击另一个按钮时播放另一个 gif 图像.. 但是当我点击第一个按钮时它正在正确播放相关图像.. 而通过单击第二个按钮,第一个图像和第二个图像将在无限循环中一个接一个地播放 ...那么如何通过单击按钮播放一张 gif 图像?

import wx, wx.animate

class MyForm(wx.Frame):

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

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

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

    #----------------------------------------------------------------------
    def onButton1(self, event):
        image='animated_1.gif'
        self.animateGIF(image)

    #----------------------------------------------------------------------
    def onButton2(self, event):
        image='animated_2.gif'
        self.animateGIF(image)

    #----------------------------------------------------------------------
    def animateGIF(self,image):
        gif = wx.animate.GIFAnimationCtrl(self, -1, image,pos=(50,70),size=(10,10))
        gif.GetPlayer()
        gif.Play()
#----------------------------------------------------------------------
app = wx.App()
frame = MyForm().Show()
app.MainLoop()

最佳答案

在开始新的 gif 图像之前,您需要停止并销毁之前的 gif 图像。像这样:

import wx, wx.animate

class MyForm(wx.Frame):

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

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

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

        self.gif = None

    #----------------------------------------------------------------------
    def onButton1(self, event):
        image='animated_1.gif'
        self.animateGIF(image)

    #----------------------------------------------------------------------
    def onButton2(self, event):
        image='animated_2.gif'
        self.animateGIF(image)

    #----------------------------------------------------------------------
    def animateGIF(self,image):
        if self.gif:
            self.gif.Stop()
            self.gif.Destroy()

        self.gif = wx.animate.GIFAnimationCtrl(self, -1, image,pos=(50,70),size=(10,10))
        self.gif.GetPlayer()
        self.gif.Play()
#----------------------------------------------------------------------
app = wx.App()
frame = MyForm().Show()
app.MainLoop()

我在 __init__ 函数中添加了 self.gif = None 并且几乎没有改变函数 animateGIF

关于python - wxpython 一张一张显示gif图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25545726/

相关文章:

python - 通过 Windows 任务计划程序安排在 Anaconda 中开发的脚本

python - wxPython 和 CEF Python 3

python - root.overrideredirect 和 <Any-KeyPress> 绑定(bind)

python - 如何在 python 中使用伪终端来模拟串口?

python - 将 NetworkX 节点属性放入 Pandas Dataframe 列

python - 如何使用 Bottle 框架进行单元测试

python - 无法在wxpython中销毁wx.Dialog

python - 在 wxPython 中使用 time.sleep

单进程的Python多处理速度

python - 如何将自定义规则添加到 spaCy 标记器以将 HTML 分解为单个标记?