python - 使用 Gtk 3 在 Python 中加载并显示来自网络的图像?

标签 python image gtk gtk3 pixbuf

我正在使用 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/

相关文章:

python - 如何在 Pygame 中将文本居中

c# - FileNotFound 当我使用 Image.FromFile()

android - 如何在自定义 ListView 中对单选按钮进行分组

python:如果它只有常量,那么实例化一个类与引用相比有什么缺点吗?

python - 如何在 Keras 中将 (None, 10) 维张量 reshape 为 (None, None, 10)?

python - pandas DataFrame 到带有内联样式而不是 CSS 的 html

javascript - 如何在多次鼠标悬停时更改光标

javascript - 如何使用 Gjs Gnome Javascript 包含文件

function - Vala 中的异步函数 - yield 和回调

树莓派交叉编译