Friday, May 3, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 4980  / 2 Years ago, sun, march 13, 2022, 9:07:12

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?


More From » application-development

 Answers
3

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_())

[#37340] Monday, March 14, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reangi

Total Points: 213
Total Questions: 102
Total Answers: 114

Location: Namibia
Member since Wed, Jan 19, 2022
2 Years ago
reangi questions
;