问题
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?
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:
Attempt 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_()
I get the following message:
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'
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_()
I get the following errror message:
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())
回答1:
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_()
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).
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.
来源:https://stackoverflow.com/questions/50209879/how-to-load-an-image-with-qlabel-in-pyside2