A QPixmap is one of the widgets used to work with images. It is optimized for showing images on screen. In our code example, we will use the QPixmap to display an image on the window.
PyQt5 利用 QtGui模块里的QPixmap实例化对象就可以将图片转换成QT组件能够识别的类型。
代码:
# coding='utf-8'
from PyQt5.QtWidgets import QApplication, QWidget,\
QLabel, QHBoxLayout
from PyQt5.QtGui import QPixmap
import sys
class Gui(QWidget):
def __init__(self):
super(Gui, self).__init__()
self.start()
def start(self):
# 利用QtGui的QPixmap创建pixmap实例化对象,\
# 给定的参数是某路径下的图标
pixmap = QPixmap('./图片素材/app图标/4.png')
# 创建水平布局hbox
hbox = QHBoxLayout(self)
# 创建父组件是主窗体的标签
label = QLabel(self)
# 给标签设置pixmap图片
label.setPixmap(pixmap)
# 将标签组件添加到水平布局中去
hbox.addWidget(label)
# 将主窗体的布局设置成hbox
self.setLayout(hbox)
self.move(300, 200)
self.setWindowTitle('图片展示')
self.show()
app = QApplication(sys.argv)
gui = Gui()
sys.exit(app.exec_())
运行结果:

网友评论