0

如何自动刷新 Gtk.Image 以从同一 URL 获取新帧?图像 now.jpeg 是每秒 1 帧,当加载 Python 脚本时,它只显示第一帧而不是更新的图像。

import os
import urllib2
from gi.repository import Gtk
from gi.repository.GdkPixbuf import Pixbuf

def quit_event(widget, event):
    os.remove(imgname)
    Gtk.main_quit()

imgname = 'now.jpeg'
url = 'http://192.168.1.11/'+imgname

response = urllib2.urlopen(url)
with open(imgname, 'wb') as img:
    img.write(response.read())

image = Gtk.Image()
pb = Pixbuf.new_from_file(imgname)
image.set_from_pixbuf(pb)

window = Gtk.Window()
window.connect('delete-event', quit_event)
window.add(image)
window.show_all()

Gtk.main()
4

1 回答 1

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

class MainWin:
    def my_timer(self):
        self.response=urllib2.urlopen(
            'http://192.168.1.11:7007/video/now.jpeg')
        self.loader=gtk.gdk.PixbufLoader()
        self.loader.write(self.response.read())
        self.loader.close()
        self.image.set_from_pixbuf(self.loader.get_pixbuf())
        return True# do ur work here, but not for long


    def destroy(self, widget, data=None):
        print "destroy signal occurred"
        gtk.main_quit()

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy", self.destroy)
        self.window.set_border_width(10)
        self.image=gtk.Image()



        gtk.timeout_add(1000, self.my_timer) # call every min

        self.window.add(self.image)
        self.image.show()
        self.window.show()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    MainWin().main()
于 2013-10-08T07:44:14.693 回答