Qlabel怎么显示图片
时间: 2023-10-21 11:08:10 浏览: 40
可以使用QPixmap将图片加载到QLabel中,示例代码如下:
```python
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QLabel, QWidget, QHBoxLayout, QApplication
import sys
class ImageLabel(QLabel):
def __init__(self, parent=None):
super(ImageLabel, self).__init__(parent)
self.setPixmap(QPixmap('image.jpg')) # 加载图片
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
layout = QHBoxLayout()
image_label = ImageLabel()
layout.addWidget(image_label)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
其中,ImageLabel继承自QLabel,通过setPixmap方法将图片加载到QLabel中。在MainWindow中,将ImageLabel添加到水平布局中,最后设置为窗口的布局。运行程序,即可在窗口中看到图片。