簡體   English   中英

Python-如何刷新Gtk.Image以從相同的URL獲取最新的圖像幀?

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

如何自動刷新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()
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()

用 Gtk 3?

[英]Load and show an image from the web in Python with Gtk 3?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM