Why is an app made using PyQt5 Designer, resized when run as python code?

  pyqt5, python, qt-designer, user-interface

So I was making an app using PyQt5 Designer and when I was done I previewed it and it looked like this:

App preview in PyQt5 Designer

but once I compiled it into Python code it looks like this when run:

Compiled python version of app

Could someone explain why this happens and help me fix this? Thanks 🙂

Edit:

I forgot to add the code so here it is.

Python code:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(400, 400)
        MainWindow.setMinimumSize(QtCore.QSize(400, 450))
        MainWindow.setMaximumSize(QtCore.QSize(400, 450))
        font = QtGui.QFont()
        font.setFamily("Ubuntu")
        MainWindow.setFont(font)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.background = QtWidgets.QWidget(self.centralwidget)
        self.background.setGeometry(QtCore.QRect(0, 0, 421, 471))
        self.background.setStyleSheet("QWidget#background{n"
        "background-color: qlineargradient(spread:pad, x1:0, y1:0, 
        x2:0.693455, y2:0.619, stop:0 rgba(52, 151, 247, 255), stop:1 
        rgba(199, 215, 231, 255));n"
        "}")
        self.background.setObjectName("background")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.background)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 20, 362, 
        411))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = 
        QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label.setMinimumSize(QtCore.QSize(359, 50))
        self.label.setMaximumSize(QtCore.QSize(359, 50))
        font = QtGui.QFont()
        font.setFamily("Ubuntu")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_2.setEnabled(True)
        self.label_2.setMinimumSize(QtCore.QSize(359, 50))
        self.label_2.setMaximumSize(QtCore.QSize(359, 50))
        font = QtGui.QFont()
        font.setFamily("Ubuntu")
        font.setPointSize(8)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.verticalLayout.addWidget(self.label_2)
        self.password = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.password.setMinimumSize(QtCore.QSize(360, 25))
        self.password.setMaximumSize(QtCore.QSize(360, 25))
        self.password.setStyleSheet("QLineEdit#password{n"
        "border-radius: 5px;n"
        "}")
        self.password.setEchoMode(QtWidgets.QLineEdit.Password)
        self.password.setObjectName("password")
        self.verticalLayout.addWidget(self.password)
        self.label_3 = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_3.setMinimumSize(QtCore.QSize(359, 50))
        self.label_3.setMaximumSize(QtCore.QSize(359, 50))
        font = QtGui.QFont()
        font.setFamily("Ubuntu")
        font.setPointSize(8)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.verticalLayout.addWidget(self.label_3)
        self.confirm_password = 
        QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.confirm_password.setMinimumSize(QtCore.QSize(360, 25))
        self.confirm_password.setMaximumSize(QtCore.QSize(360, 25))
        self.confirm_password.setStyleSheet
        ("QLineEdit#confirm_password{n"
        "border-radius: 5px;n"
        "}")
        self.confirm_password.setEchoMode(QtWidgets.QLineEdit.Password)
        self.confirm_password.setObjectName("confirm_password")
        self.verticalLayout.addWidget(self.confirm_password)
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        spacerItem = QtWidgets.QSpacerItem(40, 20, 
        QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem, 0, 2, 1, 1)
        self.continue_button = 
        QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.continue_button.setMinimumSize(QtCore.QSize(100, 25))
        self.continue_button.setMaximumSize(QtCore.QSize(100, 25))
        font = QtGui.QFont()
        font.setFamily("Ubuntu")
        self.continue_button.setFont(font)
        self.continue_button.setLayoutDirection(QtCore.Qt.LeftToRight)
        
        self.continue_button.setStyleSheet
        ("QPushButton#continue_button{n"
        "border-radius: 5px;n"
        "background-color: #ffffff;n"
        "}n"
        "n"
        "QPushButton:hover#continue_button{n"
        "background-color: rgb(167, 199, 231);n"
        "}")
        self.continue_button.setCheckable(False)
        self.continue_button.setAutoDefault(False)
        self.continue_button.setDefault(False)
        self.continue_button.setFlat(False)
        self.continue_button.setObjectName("continue_button")
        self.gridLayout.addWidget(self.continue_button, 0, 1, 1, 1)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, 
        QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem1, 0, 0, 1, 1)
        self.verticalLayout.addLayout(self.gridLayout)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", 
        "MainWindow"))
        self.label.setText(_translate("MainWindow", "Sign Up"))
        self.label_2.setText(_translate("MainWindow", "Enter a 
        password:"))
        self.password.setPlaceholderText(_translate("MainWindow", 
        "Password"))
        self.label_3.setText(_translate("MainWindow", "Confirm 
        password:"))
        self.confirm_password.setPlaceholderText
        (_translate("MainWindow", "Password"))
        self.continue_button.setText(_translate("MainWindow", 
        "Continue"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

UI file:

<?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>400</width>
    <height>450</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>400</width>
    <height>450</height>
   </size>
  </property>
  <property name="maximumSize">
   <size>
    <width>400</width>
    <height>450</height>
   </size>
  </property>
  <property name="font">
   <font>
    <family>Ubuntu</family>
   </font>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QWidget" name="background" native="true">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>421</width>
      <height>471</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">QWidget#background{
     background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0.693455, y2:0.619, stop:0 rgba(52, 151, 247, 255), stop:1 rgba(199, 215, 231, 255));
     }</string>
    </property>
    <widget class="QWidget" name="verticalLayoutWidget">
     <property name="geometry">
      <rect>
       <x>20</x>
       <y>20</y>
       <width>362</width>
       <height>411</height>
      </rect>
     </property>
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
       <widget class="QLabel" name="label">
        <property name="minimumSize">
         <size>
          <width>359</width>
          <height>50</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>359</width>
          <height>50</height>
         </size>
        </property>
        <property name="font">
         <font>
          <family>Ubuntu</family>
          <pointsize>12</pointsize>
          <weight>75</weight>
          <bold>true</bold>
         </font>
        </property>
        <property name="text">
         <string>Sign Up</string>
        </property>
        <property name="alignment">
         <set>Qt::AlignCenter</set>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QLabel" name="label_2">
        <property name="enabled">
         <bool>true</bool>
        </property>
        <property name="minimumSize">
         <size>
          <width>359</width>
          <height>50</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>359</width>
          <height>50</height>
         </size>
        </property>
        <property name="font">
         <font>
          <family>Ubuntu</family>
          <pointsize>8</pointsize>
         </font>
        </property>
        <property name="text">
         <string>Enter a password:</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QLineEdit" name="password">
        <property name="minimumSize">
         <size>
          <width>360</width>
          <height>25</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>360</width>
          <height>25</height>
         </size>
        </property>
        <property name="styleSheet">
         <string notr="true">QLineEdit#password{
        border-radius: 5px;
        }</string>
        </property>
        <property name="echoMode">
         <enum>QLineEdit::Password</enum>
        </property>
        <property name="placeholderText">
         <string>Password</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QLabel" name="label_3">
        <property name="minimumSize">
         <size>
          <width>359</width>
          <height>50</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>359</width>
          <height>50</height>
         </size>
        </property>
        <property name="font">
         <font>
          <family>Ubuntu</family>
          <pointsize>8</pointsize>
         </font>
        </property>
        <property name="text">
         <string>Confirm password:</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QLineEdit" name="confirm_password">
        <property name="minimumSize">
         <size>
          <width>360</width>
          <height>25</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>360</width>
          <height>25</height>
         </size>
        </property>
        <property name="styleSheet">
         <string notr="true">QLineEdit#confirm_password{
        border-radius: 5px;
        }</string>
        </property>
        <property name="echoMode">
         <enum>QLineEdit::Password</enum>
        </property>
        <property name="placeholderText">
         <string>Password</string>
        </property>
       </widget>
      </item>
      <item>
       <layout class="QGridLayout" name="gridLayout">
        <item row="0" column="2">
         <spacer name="horizontalSpacer">
          <property name="orientation">
           <enum>Qt::Horizontal</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>40</width>
            <height>20</height>
           </size>
          </property>
         </spacer>
        </item>
        <item row="0" column="1">
         <widget class="QPushButton" name="continue_button">
          <property name="minimumSize">
           <size>
            <width>100</width>
            <height>25</height>
           </size>
          </property>
          <property name="maximumSize">
           <size>
            <width>100</width>
            <height>25</height>
           </size>
          </property>
          <property name="font">
           <font>
            <family>Ubuntu</family>
           </font>
          </property>
          <property name="layoutDirection">
           <enum>Qt::LeftToRight</enum>
          </property>
          <property name="styleSheet">
           <string notr="true">QPushButton#continue_button{
           border-radius: 5px;
           background-color: #ffffff;}

           QPushButton:hover#continue_button{
           background-color: rgb(167, 199, 231);
           }</string>
          </property>
          <property name="text">
           <string>Continue</string>
          </property>
          <property name="checkable">
           <bool>false</bool>
          </property>
          <property name="autoDefault">
           <bool>false</bool>
          </property>
          <property name="default">
           <bool>false</bool>
          </property>
          <property name="flat">
           <bool>false</bool>
          </property>
         </widget>
        </item>
        <item row="0" column="0">
         <spacer name="horizontalSpacer_2">
          <property name="orientation">
           <enum>Qt::Horizontal</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>40</width>
            <height>20</height>
           </size>
          </property>
         </spacer>
        </item>
       </layout>
      </item>
     </layout>
    </widget>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

Source: Python Questions

LEAVE A COMMENT