Python PyQt-从URL设置QLabel图像
我一直在尝试从URL将图像设置为QLabel。但是我没有运气。 以下是我现在如何努力做到这一点:Python PyQt-从URL设置QLabel图像,python,pyqt,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(Q
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
是否有其他方法来解决此问题?采用的是文件名,而不是图像数据。如果要这样做,需要使用
QImage.loadFromData
简单公式将图像从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)
我尝试了这个方法,但得到了错误“”“QImage.loadFromData(str,str format=None):unbound方法的第一个参数必须具有类型“QImage”“”“也许我没有正确读取url以将其存储到字符串中?线索是
unbound method
:您必须在QImage
的实例上使用它,而不是在类上。真是太棒了!很有效,谢谢!问题是从URL获取图像。