问题描述
我想在我的主窗口中添加背景图片而不更改其中按钮的背景图片,并且还需要保持宽高比
I would like to add a background image to my main window without changing the background image of pushbutton in it and also need to keep the aspect ratio
我试过了
self.centralWidget.setStyleSheet("background-image: url(The_Project_logo.png); background-repeat: no-repeat; background-position: center")
但这会改变所有小部件的背景图像
but this changes the background images of all widgets
推荐答案
试试看:
from PyQt5.QtWidgets import * class MainWindow(QMainWindow): def __init__(self): super().__init__() self.centralwidget = QWidget() self.setCentralWidget(self.centralwidget) self.pushButton1 = QPushButton("Button 1", self.centralwidget) self.pushButton2 = QPushButton("Button 2", self.centralwidget) lay = QHBoxLayout(self.centralwidget) lay.addWidget(self.pushButton1) lay.addWidget(self.pushButton2) stylesheet = """ MainWindow { background-image: url("D:/_Qt/img/cat.jpg"); background-repeat: no-repeat; background-position: center; } """ if __name__ == "__main__": import sys app = QApplication(sys.argv) app.setStyleSheet(stylesheet) # <--- window = MainWindow() window.resize(640, 640) window.show() sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(400, 300) self.centralWidget = QtWidgets.QWidget(MainWindow) self.centralWidget.setObjectName("centralWidget") self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.centralWidget) self.horizontalLayout_2.setContentsMargins(11, 11, 11, 11) self.horizontalLayout_2.setSpacing(6) self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setSpacing(6) self.horizontalLayout.setObjectName("horizontalLayout") self.start_button = QtWidgets.QPushButton(self.centralWidget) self.start_button.setObjectName("start_button") self.horizontalLayout.addWidget(self.start_button) self.stop_button = QtWidgets.QPushButton(self.centralWidget) self.stop_button.setObjectName("stop_button") self.horizontalLayout.addWidget(self.stop_button) self.horizontalLayout_2.addLayout(self.horizontalLayout) MainWindow.setCentralWidget(self.centralWidget) self.menuBar = QtWidgets.QMenuBar(MainWindow) self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 26)) self.menuBar.setObjectName("menuBar") MainWindow.setMenuBar(self.menuBar) self.mainToolBar = QtWidgets.QToolBar(MainWindow) self.mainToolBar.setObjectName("mainToolBar") MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar) self.statusBar = QtWidgets.QStatusBar(MainWindow) self.statusBar.setObjectName("statusBar") MainWindow.setStatusBar(self.statusBar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.start_button.setText(_translate("MainWindow", "Start")) self.stop_button.setText(_translate("MainWindow", "Stop")) class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) stylesheet = """ QMainWindow { background-image: url("D:/_Qt/img/cat.jpg"); background-repeat: no-repeat; background-position: center; } """ if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) app.setStyleSheet(stylesheet) w = MyWindow() # MainWindow = QtWidgets.QMainWindow() # ui = Ui_MainWindow() # ui.setupUi(MainWindow) # MainWindow.show() w.show() sys.exit(app.exec_())