QFrame without mosaic and centered

2

I have the following code:

from PyQt5 import QtCore, QtWidgets, uic

from functools import partial


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        uic.loadUi("1.ui",self)

        d = {self.Inicio: InicioWidget(), self.Ventas: VentasWidget()}

        for button, window in d.items():
            self.stackedWidget.addWidget(window)
            button.clicked.connect(partial(self.stackedWidget.setCurrentWidget, window))


class InicioWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(InicioWidget, self).__init__(parent)

        frame = QtWidgets.QFrame()
        frame.setStyleSheet("background-image:url(Logo1.png);")


        grid = QtWidgets.QGridLayout()

       #grid.addWidget(title,0,1)
        grid.addWidget(frame,0,0)

        self.setLayout(grid)

class VentasWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(VentasWidget, self).__init__(parent)
        self.setLayout(QtWidgets.QVBoxLayout())
        self.layout().addWidget(QtWidgets.QLabel("Ventas\nVentana"))
        self.setLayout(QtWidgets.QHBoxLayout())
        self.layout().addWidget(QtWidgets.QLabel("Ventas\nVentana"))
        self.setLayout(QtWidgets.QGridLayout())
        self.layout().addWidget(QtWidgets.QLabel("Ventas\nVentana"))





if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <property name="styleSheet">
   <string notr="true"/>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <property name="leftMargin">
     <number>0</number>
    </property>
    <property name="topMargin">
     <number>0</number>
    </property>
    <property name="rightMargin">
     <number>0</number>
    </property>
    <property name="bottomMargin">
     <number>0</number>
    </property>
    <item>
     <widget class="QFrame" name="topframe">
      <property name="styleSheet">
       <string notr="true">QFrame#topframe{
    background:qlineargradient(spread:pad, x1:0.384, y1:1, x2:1, y2:0, stop:0 rgba(0, 0, 0, 127), stop:1 rgba(255, 255, 255, 15))
}

QFrame#topframe &gt; QPushButton {
    border:0px;
    border-right:2px solid #00cc00;
}

QFrame#topframe &gt; QPushButton:hover {
    border-right:2px solid blue;
    background:qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(0, 0, 0, 93), stop:1 rgba(255, 255, 255, 153));
}</string>
      </property>
      <property name="frameShape">
       <enum>QFrame::StyledPanel</enum>
      </property>
      <property name="frameShadow">
       <enum>QFrame::Raised</enum>
      </property>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <property name="leftMargin">
        <number>2</number>
       </property>
       <property name="topMargin">
        <number>2</number>
       </property>
       <property name="rightMargin">
        <number>0</number>
       </property>
       <property name="bottomMargin">
        <number>2</number>
       </property>
       <item>
        <widget class="QPushButton" name="Inicio">
         <property name="styleSheet">
          <string notr="true"/>
         </property>
         <property name="text">
          <string>Inicio</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="Ventas">
         <property name="text">
          <string>Ventas</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="Registro">
         <property name="text">
          <string>Registro</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="Consulta">
         <property name="text">
          <string>Consulta</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="Mensajeria">
         <property name="text">
          <string>Mensajeria</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="Salir">
         <property name="text">
          <string>Salir</string>
         </property>
        </widget>
       </item>
       <item>
        <spacer name="spacer">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeType">
          <enum>QSizePolicy::Fixed</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>316</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
      </layout>
     </widget>
    </item>
    <item>
     <widget class="QStackedWidget" name="stackedWidget">
      <property name="currentIndex">
       <number>-1</number>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

I need the frame that I placed to be shown in the center of the screen with the image that I placed in the background:

but I get this result:

I hope you can help me

    
asked by Revsky01 12.07.2018 в 19:32
source

1 answer

2

You have to establish through qss that the image is not repeated and be centered with background-repeat and background-position , respectively.

class InicioWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(InicioWidget, self).__init__(parent)
        self.frame = QtWidgets.QFrame()
        self.frame.setStyleSheet("""background-image: url(Logo1.png); 
                                    background-repeat: None; 
                                    background-position: center""")
        grid = QtWidgets.QGridLayout(self)
        grid.addWidget(self.frame,0,0)

    
answered by 13.07.2018 / 17:47
source