如何自动刷新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()
答案 0 :(得分: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()