2

I am developing Discvur, a simple Imgur viewer, for the Ubuntu App Showdown. Therefore I use quickly + Glade + Gtk + Python. PyGObject instead of PyGtk would be highly appreciated. I would like to display some animated gifs in my program.

What I tried was inserting a viewport, then an image, and then in the 'Edit Image' field I selected a gif animation (in my case ../media/akO1i.gif).

When I run my app, the gif is displayed but it is not moving (I only see the first frame).

Is it possible to show animated gifs in my app? What is the best and/or easiest way to do it: use the image widget, or a WebKit field, or something else?

2 Answers 2

4

Since you've added GTK to your question, example and documentation can be found at the PyGTK 2.0 Tutorial on the Chapter 9. Miscellaneous Widgets.

A code sample using .gif for buttons:

#!/usr/bin/env python

# example images.py

import pygtk
pygtk.require('2.0')
import gtk

class ImagesExample:
    # when invoked (via signal delete_event), terminates the application.
    def close_application(self, widget, event, data=None):
        gtk.main_quit()
        return False

    # is invoked when the button is clicked.  It just prints a message.
    def button_clicked(self, widget, data=None):
        print "button %s clicked" % data

    def __init__(self):
        # create the main window, and attach delete_event signal to terminating
        # the application
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("delete_event", self.close_application)
        window.set_border_width(10)
        window.show()

        # a horizontal box to hold the buttons
        hbox = gtk.HBox()
        hbox.show()
        window.add(hbox)

        pixbufanim = gtk.gdk.PixbufAnimation("goalie.gif")
        image = gtk.Image()
        image.set_from_animation(pixbufanim)
        image.show()
        # a button to contain the image widget
        button = gtk.Button()
        button.add(image)
        button.show()
        hbox.pack_start(button)
        button.connect("clicked", self.button_clicked, "1")
        
        image = gtk.Image()
        image.set_from_file("soccerball.gif")
        image.show()
        # a button to contain the image widget
        button = gtk.Button()
        button.add(image)
        button.show()
        hbox.pack_start(button)
        button.connect("clicked", self.button_clicked, "2")


def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    ImagesExample()
    main()

ORIGINAL ANSWER

You can use PyQt with the QMovie() widget to play an animated gif. Bellow an example that I've found earlier.

Example from Python GUI Programming | DaniWeb web-site:

# use PyQt's QMovie() widget to play an animated gif
# tested with PyQt4.4 and Python 2.5
# also tetsed with PyQt4.5 and Python 3.0
# vegaseat

import sys
# expect minimal namespace conflicts
from PyQt4.QtCore import *
from PyQt4.QtGui import * 

class MoviePlayer(QWidget):
    def __init__(self, parent=None): 

    QWidget.__init__(self, parent)
    # setGeometry(x_pos, y_pos, width, height)
    self.setGeometry(200, 200, 400, 300)
    self.setWindowTitle("QMovie to show animated gif")

    # set up the movie screen on a label
    self.movie_screen = QLabel()
    # expand and center the label
    self.movie_screen.setSizePolicy(QSizePolicy.Expanding,
    QSizePolicy.Expanding)
    self.movie_screen.setAlignment(Qt.AlignCenter) 

    main_layout = QVBoxLayout()
    main_layout.addWidget(self.movie_screen)
    self.setLayout(main_layout)

    # use an animated gif file you have in the working folder
    # or give the full file path
    movie = QMovie("AG_Dog.gif", QByteArray(), self)
    movie.setCacheMode(QMovie.CacheAll)
    movie.setSpeed(100)
    self.movie_screen.setMovie(movie)
    movie.start() 

app = QApplication(sys.argv)
player = MoviePlayer()
player.show()
sys.exit(app.exec_()) 
5
  • Thank you but I would prefer to stay with Gtk since I already started using and learning it. You answer is probably excellent for other people.
    – Agmenor
    Jun 23, 2012 at 17:36
  • You didn't mentioned GTK before :) Just added an update to the top of my answer with an example, link to tutorials and documentation.
    – Zuul
    Jun 23, 2012 at 17:53
  • This worked perfectly. Thank you for the script and for the link to the tutorial.
    – Agmenor
    Jun 23, 2012 at 18:06
  • Could you maybe give me some more information about how to do it with PyGObject instead of PyGtk? I did from gi.repository import Gdk but the package "PixbufAnimation" is not in there.
    – Agmenor
    Jun 23, 2012 at 18:51
  • Sure, here's a link to documentation and examples, and a link to Porting PyGTK to PyGObject.
    – Zuul
    Jun 23, 2012 at 20:07
1

This is the code I finally wrote, thanks to Zuul's help. It is more specific to PyGObject, the tool that is used in Quickly:

from gi.repository import Gtk, GdkPixbuf
#This is specific to my app Discvur developed in Quickly:
from discvur_lib.discvurconfig import get_data_file

[…]

    self.viewport = self.builder.get_object("viewport")
    path = get_data_file() + "/media/akO1i.gif"
    print path
    self.pixbufanim = GdkPixbuf.PixbufAnimation.new_from_file(path)
    print self.pixbufanim
    self.image = Gtk.Image()
    self.image.set_from_animation(self.pixbufanim)
    self.viewport.add(self.image)
    self.image.show()

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .