本例程使用PyQt5库,在QMainWindow中设置背景图片。重要的是两步: mainwindow.setObjectName("MainWindow") #设置对象名称 mainwindow.setStyleSheet("#MainWindow{border-image:url(./python.jpg);}") #设置窗口背景图片 以下为示例代码: #-*-coding:utf-8-*- from PyQt5.QtWidgets import QMainWindow,QApplication,QToolBar,QFrame,QAction,QFileDialog from PyQt5.QtGui import QIcon,QPixmap import sys class PixmapDemo(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): filetb = self.addToolBar("File") open=QAction(QIcon('open.png'),'open',self) filetb.addAction(open) filetb.actionTriggered[QAction].connect(self.toolbtnpressed) self.setObjectName("MainWindow") #设置对象名称 #设置窗口背景图片 self.setStyleSheet("#MainWindow{border-image:url(./python.jpg);}") self.setWindowTitle("QPixmap示例") self.setGeometry(100,100,300,200) self.show() def toolbtnpressed(self): fname = QFileDialog.getOpenFileName(self, '选择图片文件', './',"jpg (*.jpg)|png(*.png)") if fname[0]: pstr ="#MainWindow{border-image:url("+str(fname[0])+");}" self.setStyleSheet(pstr) if __name__ == "__main__": app = QApplication(sys.argv) pm_w = PixmapDemo() sys.exit(app.exec_()) 运行结果如下: