python - PyQt - 从 URL 设置 QLabel 图像

标签 python pyqt

我一直在尝试从 URL 将图像设置为 QLabel。但我没有运气。 这是我现在尝试做的事情:

import urllib, cStringIO
img_file = cStringIO.StringIO(urllib.urlopen(image_url).read())
image_file = Image.open(img_file)

然后将其设置为 QImage:

final_image = QImage(image_file)

self.emit(SIGNAL("finished(QImage)"),
    final_image
)

图像正在从一个线程返回到主 GUI 中的一个方法。

def set_image(self, final_image):
    self.main_picture_pixmap = QPixmap.fromImage(final_image).scaled(
        QSize(self.picture_label.size()), 
        Qt.KeepAspectRatio, 
        Qt.FastTransformation
    )
    self.picture_label.setPixmap(self.main_picture_pixmap)

这样做我得到错误:

QPixmap::scaled: Pixmap is a null pixmap

有没有办法解决这个问题,或者有其他方法可以解决这个问题?

最佳答案

将图片从 url 加载到图标中的简单公式。

from PyQt4.QtGui import QPixmap, QIcon
import urllib

url = 'http://example.com/image.png'    
data = urllib.urlopen(url).read()
pixmap = QPixmap()
pixmap.loadFromData(data)
icon = QIcon(pixmap)

关于python - PyQt - 从 URL 设置 QLabel 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11073972/

相关文章:

python - PyQt5 GUI - 使用 PyInstaller 制作的 exe 无法打开

python - 传感器读取覆盆子数据

python - pyqt 示例 : an image viewer 的奇怪结果

python - 如何检测 QInputDialog 中值的变化?

python - 如何使用 OpenCV、人脸识别、Flask、Python 3 和 Heroku 在客户端打开摄像头

python - Pytest:请求 fixture 的嵌套使用

python - 调整窗口大小时垂直增长小部件(使用网格几何管理器)

python - 使用 pysftp 和 Python 3 仅使用服务器指纹连接到 SFTP 服务器

python - python 更新后 Pygame 变慢

javascript - 来自 QWebPage 的 QImage 未完全呈现