user285594
user285594

Reputation:

Python - how to refresh the Gtk.Image to get the latest image frame from same URL?

How to auto refresh the Gtk.Image to get new frames from that same URL? The image now.jpeg is 1 frame per second, when loading the Python script, it only show first frame not the updated images.

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

Upvotes: 0

Views: 1687

Answers (1)

user285594
user285594

Reputation:

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

Upvotes: 2

Related Questions