我正在使用 Python 和 GTK 3 在 Ubuntu 12.04 上编写一个应用程序。我遇到的问题是我无法弄清楚我应该如何在我的应用程序中使用来自网络的图像文件显示 Gtk.Image .
这就是我所到之处:
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))
除了最后一行,我认为一切都是正确的。
最佳答案
这会起作用;
from gi.repository import Gtk
from gi.repository.GdkPixbuf import Pixbuf
from gi.repository import Gio
import urllib
url = 'http://lolcat.com/images/lolcats/1338.jpg'
response = urllib.request.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)
关于python - 使用 Gtk 3 在 Python 中加载并显示来自网络的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11549480/