Pyqt5 简明教程
PyQt5 - QPixmap Class
QPixmap 类提供图像的非屏幕表示。它可以用作 QPaintDevice 对象,也可以加载到另一个小部件,通常是标签或按钮。
QPixmap class provides an off-screen representation of an image. It can be used as a QPaintDevice object or can be loaded into another widget, typically a label or button.
Qt API 还有另一个类似类 QImage ,经过优化,可进行 I/O 及其他像素操作。另一方面,Pixmap 已针对显示在屏幕上进行了优化。两种格式可以相互转换。
Qt API has another similar class QImage, which is optimized for I/O and other pixel manipulations. Pixmap, on the other hand, is optimized for showing it on screen. Both formats are interconvertible.
可以读入 QPixmap 中的图像文件类型如下 -
The types of image files that can be read into a QPixmap object are as follows −
BMP |
Windows Bitmap |
GIF |
Graphic Interchange Format (optional) |
JPG |
Joint Photographic Experts Group |
JPEG |
Joint Photographic Experts Group |
PNG |
Portable Network Graphics |
PBM |
Portable Bitmap |
PGM |
Portable Graymap |
PPM |
Portable Pixmap |
XBM |
X11 Bitmap |
XPM |
X11 Pixmap |
以下方法对于处理 QPixmap 对象很有用 -
Following methods are useful in handling QPixmap object −
Sr.No. |
Methods & Description |
1 |
copy() Copies pixmap data from a QRect object |
2 |
fromImage() Converts QImage object into QPixmap |
3 |
grabWidget() Creates a pixmap from the given widget |
4 |
grabWindow() Create pixmap of data in a window |
5 |
Load() Loads an image file as pixmap |
6 |
save() Saves the QPixmap object as a file |
7 |
toImage Converts a QPixmap to QImage |
QPixmap 最常见的用途是在标签/按钮上显示图像。
The most common use of QPixmap is to display image on a label/button.
Example
以下示例显示了通过使用 setPixmap() 方法在 QLabel 上显示的图像。
The following example shows an image displayed on a QLabel by using the setPixmap() method.
完整代码如下所示:
The complete code is as follows −
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
def window():
app = QApplication(sys.argv)
win = QWidget()
l1 = QLabel()
l1.setPixmap(QPixmap("python.png"))
vbox = QVBoxLayout()
vbox.addWidget(l1)
win.setLayout(vbox)
win.setWindowTitle("QPixmap Demo")
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
window()
上述代码生成以下输出 -
The above code produces the following output −
