如何在我正在开发的应用程序中显示动画 gif?

如何在我正在开发的应用程序中显示动画 gif?

我正在开发迪斯科,一个简单的 Imgur 查看器,用于 Ubuntu App Showdown。因此我使用 quick + Glade + Gtk + Python。PyGObject 而不是 PyGtk 将受到高度赞赏。我想在我的程序中显示一些动画 gif。

我尝试插入一个视口,然后插入一张图片,然后在“编辑图像”字段中选择一个 gif 动画(在我的情况下../media/akO1i.gif)。

当我运行我的应用程序时,gif 显示了但它没有移动(我只看到第一帧)。

是否可以在我的应用中显示动画 GIF?最佳和/或最简单的方法是什么:使用图像小部件、WebKit 字段还是其他方法?

答案1

由于您已将 GTK 添加到您的问题中,示例和文档可以在 PyGTK 2.0 教程中找到第 9 章 杂项小部件

A代码示例用于.gif按钮:

#!/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()

原始答案

您可以使用介绍QMovie()窗口小部件播放动画 gif。下面是我之前找到的一个例子。

示例来自Python GUI 编程 | DaniWeb网站:

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

答案2

这是我最终编写的代码,感谢Zuul 的帮助。它更具体地针对 PyGObject,即 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()

相关内容