wxPython Animated Gif

vegaseat 1 Tallied Votes 2K Views Share

Version 2.6 of wxPython has support for playing animated gif image files. The example sets up a panel on a frame and then uses wx.animate.GIFAnimationCtrl() to do the hard work. Actually pretty simple code.

# display an animated GIF image file using wxPython
# tested with Python24 and wxPython26    vegaseat   22nov2005

import wx
import wx.animate     # ..\wx\animate.py

class MyPanel(wx.Panel):
    """ class MyPanel creates a panel, inherits wx.Panel """
    def __init__(self, parent, id):
        # default pos and size creates a panel that fills the frame
        wx.Panel.__init__(self, parent, id)
        self.SetBackgroundColour("white")
        # pick the filename of an animated GIF file you have ...
        # give it the full path and file name!
        ag_fname = "D:/Python24/Atest/wx/AG_Dog.gif"
        ag = wx.animate.GIFAnimationCtrl(self, id, ag_fname, pos=(10, 10))
        # clears the background
        ag.GetPlayer().UseBackgroundColour(True)
        # continuously loop through the frames of the gif file (default)
        ag.Play()


app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
# give it a size so the image will fit ...
frame = wx.Frame(None, -1, "wx.animate.GIFAnimationCtrl()", size = (500, 400))
# call the derived class, -1 is default ID
MyPanel(frame, -1)
# show the frame
frame.Show(True)
# start the event loop
app.MainLoop()
samstag 0 Newbie Poster

this one helped me out very much. Thanx! I just wanted to go a lil step further and wanted the user to choose the anim.Gif from a directory.
I did this, with Tkinter File Dialog, by changing line 15 into:
[ ag_fname = tkFileDialog.askopenfilename (initialdir=[('C:/gif_python')]) ]

i works, till i hit the stop button, the gif-frame stops but the tiny Tk-win wont stop.

this is the full code:

import wx
import wx.animate
from Tkinter import *
import tkFileDialog

class gifFrame(wx.Frame):
### Basic Frame,
def __init__(self, parent, id):
wx.Frame.__init__(self,parent,id,"FRAME 1 aka Panel, Window",
size=(800,600))
### EXTRAS ###
panel=wx.Panel(self)
b1=wx.Button(panel, label = " STOP ", pos = (380, 500))
self.Bind(wx.EVT_BUTTON, self.closebutton, b1)
## GIF
ag_fname = tkFileDialog.askopenfilename (initialdir=[('C:/gif_python')])
gif1 = wx.animate.GIFAnimationCtrl(panel, id, ag_fname, pos=(75,10),
size=(200,200))
gif1.Play()

### Funktion Button STOP
def closebutton(self,event):
self.Close(True)


# show FRAME
if __name__=='__main__':
app=wx.PySimpleApp()
frame=gifFrame(parent=None,id=-1)
frame.Show()
app.MainLoop()
Gribouillis 1,391 Programming Explorer Team Colleague

It's a really strange idea to mix wxpython and tkinter in the same program. You should probably use a wx.FileDialog. See the FileDialog example in this page http://wiki.wxpython.org/Getting%20Started#Dialogs

woooee 814 Nearly a Posting Maven

the gif-frame stops but the tiny Tk-win wont stop

The stop button is only bound to the wxpython widgets. You would have to have a separate stop button in Tkinter, or as stated above, the usual process is to code everything using the same tool kit.

samstag 0 Newbie Poster

thanks 4 the fast replies, thanks alot. I just startet coding 2 months ago and I used to work (learn) on python3.2 with Tk. As i discovered this snippet i've downgraded to 2.7 cause i didn't get wx to work in 3.2 (can't remember excatly). anyways: i have to learn much much more.
I tried wx.Python before mixing it with TK.
With wx.FileDialog i can't even open the gif. I get this Type Error.

TypeError: String or Unicode type required
Gribouillis 1,391 Programming Explorer Team Colleague

Can you post the whole code ?

samstag 0 Newbie Poster

i got it now. Next step will be play/pause buttons.

# -*- coding: cp1252 -*-
import wx
import wx.animate


class gifFrame(wx.Frame):
    ### Basic Frame, 
    def __init__(self, parent, id):
        wx.Frame.__init__(self,parent,id,"FRAME 1 aka Panel, Window",
                          size=(800,600))
        ### EXTRAS ###
        panel=wx.Panel(self)
        b1=wx.Button(panel, label = " STOP ", pos = (380, 500))
        self.Bind(wx.EVT_BUTTON, self.closebutton, b1)        
        ## GIF
        ag_fname = wx.FileDialog(self, defaultFile="", style=wx.OPEN)
        if ag_fname.ShowModal() == wx.ID_OK:
            gif = ag_fname.GetPath()
        
        gif1 = wx.animate.GIFAnimationCtrl(panel, id, gif, pos=(75,10),
                                            size=(200,200))
        gif1.Play()
        
    ### Funktion Button STOP
    def closebutton(self,event):
        self.Close(True)
        
        
# show FRAME 
if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=gifFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
woooee 814 Nearly a Posting Maven

Indents are incorrect

ag_fname = wx.FileDialog(self, defaultFile="", style=wx.OPEN)
        if ag_fname.ShowModal() == wx.ID_OK:
            gif = ag_fname.GetPath()
 
            gif1 = wx.animate.GIFAnimationCtrl(panel, id, gif, pos=(75,10),
                                            size=(200,200))
            gif1.Play()
        else:
            print "Could not find the file"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.