如何在PySide2中使用QLabel加载图像 [英] How to load an image with QLabel in PySide2

查看:2491
本文介绍了如何在PySide2中使用QLabel加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码和python的初学者.我读到,如果您要开发稍微复杂一点的应用程序,并且PyQt的许可问题很大,则tkinter有点基本".这就是为什么我选择PySide2来开发

I'm a beginner in coding and python. I read that tkinter is a bit to "basic" if you want to develop an application which is a a bit more complicated and PyQt is problematic for licencing. This is why I chose PySide2 in order to develop this kind of project, but found so far the documentation relatively scarce. Is this the right choice?

我当前的编码问题如下:我试图用PySide2加载图像而没有成功.这是我两次尝试的代码:

My present coding problem is the following: I'm Trying to load an image with PySide2 without success. Here is the code of my 2 attempts:

尝试N°1:

import sys
import PySide2
from PySide2.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)
img = PySide2.QtGui.QImage("/Users/mymac/Downloads/ecg_measure.png")
pixmap=PySide2.QtGui.QPixmap(img)
label = PySide2.QtWidgets.QLabel.setPixmap(pixmap)
label.show()
app.exec_()

我收到以下消息:

Traceback (most recent call last):

File "<ipython-input-1-86961df4959d>", line 8, in <module>
label = PySide2.QtWidgets.QLabel.setPixmap(pixmap)

TypeError: descriptor 'setPixmap' requires a 'PySide2.QtWidgets.QLabel' object but received a 'PySide2.QtGui.QPixmap'

使用回溯中的信息,我尝试了以下尝试N°2:

Using the information in the Traceback, I tried the following Attempt N°2:

import sys
import PySide2
from PySide2.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)
img = PySide2.QtGui.QImage("/Users/mymac/Downloads/ecg_measure.png")
pixmap=PySide2.QtGui.QPixmap(img)
lab=PySide2.QtWidgets.QLabel(pixmap)
PySide2.QtWidgets.QLabel.setPixmap(lab)
app.exec_()

我收到以下错误消息:

Traceback (most recent call last):

File "<ipython-input-1-61b41e4b2f63>", line 8, in <module>
lab=PySide2.QtWidgets.QLabel(pixmap)

TypeError: 'PySide2.QtWidgets.QLabel' called with wrong argument types:
PySide2.QtWidgets.QLabel(PySide2.QtGui.QPixmap) Supported signatures:
PySide2.QtWidgets.QLabel(PySide2.QtWidgets.QWidget = None, 
PySide2.QtCore.Qt.WindowFlags = Qt.WindowFlags())
PySide2.QtWidgets.QLabel(unicode, PySide2.QtWidgets.QWidget = None, PySide2.QtCore.Qt.WindowFlags = Qt.WindowFlags())

推荐答案

这是您的代码应为的样子:

Here is what your code should look like:

import sys
from PySide2 import QtCore, QtGui, QtWidgets

app = QtWidgets.QApplication(sys.argv)

pixmap = QtGui.QPixmap('/Users/mymac/Downloads/ecg_measure.png')
label = QtWidgets.QLabel()
label.setPixmap(pixmap)    
label.show()

app.exec_()

但是,QLabel可能不适合您要开发的项目.您很可能需要使用图形视图框架.请参阅此图像查看器示例,以获取提供平移和缩放功能的基本演示.它是使用PyQt5编写的,但是您可以轻松地将其转换为PySide2-只需在文件顶部的导入中将PyQt5替换为PySide2,然后在第4行将pyqtSignal替换为Signal.在当前目录中需要一个名为"image.jpg"的图像文件(或者您可以只在loadImage方法中编辑默认路径).

However, a QLabel is probably not suitable for the project you want to develop. You will most likely need to use the Graphics View Framework. See this image viewer example for a basic demo that provides pan and zoom. It is written using PyQt5, but you can easily convert it to PySide2 - just replace PyQt5 with PySide2 in the imports at the top of the file, and replace pyqtSignal with Signal on line 4. The example also requires an image file named "image.jpg" in the current directory (or you can just edit the default path in the loadImage method).

如果您对PySide/PyQt没有太多经验,我建议您通过本教程进行操作.它是为PyQt5编写的,但是PySide2的代码将具有99%的相同性.如上所述,您通常只需要更改导入并删除API的一些pyqt前缀,例如pyqtSignalpyqtSlot.完整的PySide2文档可以在此处找到,而完整的Qt文档是此处.

If you do not have much experience with PySide/PyQt, I would recommend that you work through this tutorial. It is written for PyQt5, but the code will be 99% identical for PySide2. As suggested above, you will usually only need to change the imports and remove a few pyqt prefixes for APIs such as pyqtSignal and pyqtSlot. The full PySide2 documentation can be found here, and the full Qt documentation is here.

这篇关于如何在PySide2中使用QLabel加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆