如何使用PyQt显示图像?

2024-04-24 17:09:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经写了下面的代码,并访问了许多教程/网站,了解如何显示图像。下面的代码似乎运行没有错误,但什么都没有显示?图像文件与python代码文件位于同一目录中。图像文件是名为“hello”的PNG图像

代码如下所示:

import sys, math, random, os
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        layout = QtGui.QVBoxLayout(self)
        pic = QtGui.QLabel(self)
        pic.setGeometry(10, 10, 400, 100)
        #use full ABSOLUTE path to the image, not relative
        pic.setPixmap(QtGui.QPixmap(os.getcwd() + "C:\\Users\\Hamzah\\My Documents\\Work\\A-Level\\USB Stuff\\Coding For Project Starter/hello.png"))
        layout.addWidget(pic)
        self.setLayout(layout)

if __name__ == "__main__":
    app = QtGui.QApplication([])
    window = Window()
    window.showFullScreen()
    app.exec_()

Tags: 代码图像importselfapphelloinitos