使用Gtk 3在Python中加载并显示来自Web的图像?

时间:2022-03-19 10:43:40

I'm writing an app on Ubuntu 12.04 with Python and GTK 3. The problem I have is that I can't figure out how I should do to show a Gtk.Image in my app with an image file from the web.

我正在使用Python和GTK 3在Ubuntu 12.04上编写应用程序。我遇到的问题是我无法弄清楚如何在我的应用程序中使用来自Web的图像文件显示Gtk.Image。

This is as far as I have come:

这是我来的:

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

url = 'http://lolcat.com/images/lolcats/1338.jpg'
response = urllib2.urlopen(url)
image = Gtk.Image()
image.set_from_pixbuf(Pixbuf.new_from_stream(response))

I think everything is correct except the last line.

除了最后一行,我认为一切都是正确的。

2 个解决方案

#1


1  

I haven't found any documentation on PixBuf. Therefore, I can't answer which arguments new_from_stream takes. For the record, the error message I was given was

我还没有找到关于PixBuf的任何文档。因此,我无法回答new_from_stream采用的参数。为了记录,我给出的错误信息是

TypeError: new_from_stream() takes exactly 2 arguments (1 given)

TypeError:new_from_stream()只需2个参数(给定1个)

But I can give you a simple solution which might even improve your application. Saving the image to a temporary file includes caching.

但我可以给你一个简单的解决方案,甚至可以改善你的应用程序。将映像保存到临时文件包括缓存。

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

url = 'http://lolcat.com/images/lolcats/1338.jpg'
response = urllib2.urlopen(url)
fname = url.split("/")[-1]
f = open(fname, "wb")
f.write(response.read())
f.close()
response.close()
image = Gtk.Image()
image.set_from_pixbuf(Pixbuf.new_from_file(fname))

I'm aware it's not the cleanest code (URL could be malformed, resource opening could fail, ...) but it should be obvious whats's the idea behind.

我知道这不是最干净的代码(URL可能格式不正确,资源开放可能会失败,......)但是显而易见的是这背后的想法是什么。

#2


7  

This will work;

这将有效;

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

url = 'http://lolcat.com/images/lolcats/1338.jpg' 
response = urllib2.urlopen(url) 
input_stream = Gio.MemoryInputStream.new_from_data(response.read(), None) 
pixbuf = Pixbuf.new_from_stream(input_stream, None) 
image = Gtk.Image() 
image.set_from_pixbuf(pixbuf)

#1


1  

I haven't found any documentation on PixBuf. Therefore, I can't answer which arguments new_from_stream takes. For the record, the error message I was given was

我还没有找到关于PixBuf的任何文档。因此,我无法回答new_from_stream采用的参数。为了记录,我给出的错误信息是

TypeError: new_from_stream() takes exactly 2 arguments (1 given)

TypeError:new_from_stream()只需2个参数(给定1个)

But I can give you a simple solution which might even improve your application. Saving the image to a temporary file includes caching.

但我可以给你一个简单的解决方案,甚至可以改善你的应用程序。将映像保存到临时文件包括缓存。

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

url = 'http://lolcat.com/images/lolcats/1338.jpg'
response = urllib2.urlopen(url)
fname = url.split("/")[-1]
f = open(fname, "wb")
f.write(response.read())
f.close()
response.close()
image = Gtk.Image()
image.set_from_pixbuf(Pixbuf.new_from_file(fname))

I'm aware it's not the cleanest code (URL could be malformed, resource opening could fail, ...) but it should be obvious whats's the idea behind.

我知道这不是最干净的代码(URL可能格式不正确,资源开放可能会失败,......)但是显而易见的是这背后的想法是什么。

#2


7  

This will work;

这将有效;

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

url = 'http://lolcat.com/images/lolcats/1338.jpg' 
response = urllib2.urlopen(url) 
input_stream = Gio.MemoryInputStream.new_from_data(response.read(), None) 
pixbuf = Pixbuf.new_from_stream(input_stream, None) 
image = Gtk.Image() 
image.set_from_pixbuf(pixbuf)