• <legend id='cVsLB'><style id='cVsLB'><dir id='cVsLB'><q id='cVsLB'></q></dir></style></legend>

      • <bdo id='cVsLB'></bdo><ul id='cVsLB'></ul>
      <i id='cVsLB'><tr id='cVsLB'><dt id='cVsLB'><q id='cVsLB'><span id='cVsLB'><b id='cVsLB'><form id='cVsLB'><ins id='cVsLB'></ins><ul id='cVsLB'></ul><sub id='cVsLB'></sub></form><legend id='cVsLB'></legend><bdo id='cVsLB'><pre id='cVsLB'><center id='cVsLB'></center></pre></bdo></b><th id='cVsLB'></th></span></q></dt></tr></i><div id='cVsLB'><tfoot id='cVsLB'></tfoot><dl id='cVsLB'><fieldset id='cVsLB'></fieldset></dl></div>

        <tfoot id='cVsLB'></tfoot>

        <small id='cVsLB'></small><noframes id='cVsLB'>

      1. 如何在不影响 Pyqt5 中的小部件的情况下将背景图

        时间:2023-08-04
          <bdo id='nmtvX'></bdo><ul id='nmtvX'></ul>
          • <small id='nmtvX'></small><noframes id='nmtvX'>

              <tbody id='nmtvX'></tbody>
            <legend id='nmtvX'><style id='nmtvX'><dir id='nmtvX'><q id='nmtvX'></q></dir></style></legend>
          • <tfoot id='nmtvX'></tfoot>
            <i id='nmtvX'><tr id='nmtvX'><dt id='nmtvX'><q id='nmtvX'><span id='nmtvX'><b id='nmtvX'><form id='nmtvX'><ins id='nmtvX'></ins><ul id='nmtvX'></ul><sub id='nmtvX'></sub></form><legend id='nmtvX'></legend><bdo id='nmtvX'><pre id='nmtvX'><center id='nmtvX'></center></pre></bdo></b><th id='nmtvX'></th></span></q></dt></tr></i><div id='nmtvX'><tfoot id='nmtvX'></tfoot><dl id='nmtvX'><fieldset id='nmtvX'></fieldset></dl></div>

                  本文介绍了如何在不影响 Pyqt5 中的小部件的情况下将背景图像添加到主窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想在我的主窗口中添加背景图片而不更改其中按钮的背景图片,并且还需要保持宽高比

                  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_())
                  

                  这篇关于如何在不影响 Pyqt5 中的小部件的情况下将背景图像添加到主窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:从不同文件中的其他类更改标签 下一篇:如何制作动态组合框PYQT5

                  相关文章

                  最新文章

                  <legend id='m1fdc'><style id='m1fdc'><dir id='m1fdc'><q id='m1fdc'></q></dir></style></legend>
                  • <bdo id='m1fdc'></bdo><ul id='m1fdc'></ul>

                    <small id='m1fdc'></small><noframes id='m1fdc'>

                  1. <tfoot id='m1fdc'></tfoot>

                  2. <i id='m1fdc'><tr id='m1fdc'><dt id='m1fdc'><q id='m1fdc'><span id='m1fdc'><b id='m1fdc'><form id='m1fdc'><ins id='m1fdc'></ins><ul id='m1fdc'></ul><sub id='m1fdc'></sub></form><legend id='m1fdc'></legend><bdo id='m1fdc'><pre id='m1fdc'><center id='m1fdc'></center></pre></bdo></b><th id='m1fdc'></th></span></q></dt></tr></i><div id='m1fdc'><tfoot id='m1fdc'></tfoot><dl id='m1fdc'><fieldset id='m1fdc'></fieldset></dl></div>