我一直在尝试从 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/