0

I am currently developing a program to calculate quote prices for things at my job. On my computer I have had no problems testing my application, and everything has actually been running smoothly. Once I run it on a different system, however, all of the widgets get cut off and squeezed together. I don't currently have a screenshot of what the problem looks like, but

this

is what the window is supposed to look like (and does on my system). I've pasted the code for that window below, and I'll try and get a screenshot of what's happening soon.

class BatteryWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(BatteryWindow, self).__init__()

        self.central_widget = QtWidgets.QWidget()
        self.central_layout = QtWidgets.QGridLayout(self.central_widget)

        self.middle_section = QtWidgets.QWidget(self.central_widget)
        self.middle_layout = QtWidgets.QGridLayout(self.middle_section)

        self.window_layout = QtWidgets.QGridLayout()

        font = QtGui.QFont()
        font.setPointSize(28)
        self.battery_quote_label = QtWidgets.QLabel(self.central_widget)
        self.battery_quote_label.setFont(font)
        self.window_layout.addWidget(self.battery_quote_label, 0, 0, 1, 1)

        font = QtGui.QFont()
        font.setPointSize(16)
        self.price_label = QtWidgets.QLabel(self.middle_section)
        self.price_label.setFont(font)
        self.middle_layout.addWidget(self.price_label, 0, 0, 1, 1)

        self.price_entry = QtWidgets.QLineEdit(self.middle_section)
        self.price_entry.setMaximumSize(QtCore.QSize(80, 31))
        self.price_entry.setFont(font)
        self.price_entry.setAlignment(QtCore.Qt.AlignCenter)
        self.middle_layout.addWidget(self.price_entry, 0, 1, 1, 1)

        self.num_batteries_label = QtWidgets.QLabel(self.middle_section)
        self.num_batteries_label.setFont(font)
        self.middle_layout.addWidget(self.num_batteries_label, 0, 6, 1, 1)

        self.num_batteries_select = QtWidgets.QSpinBox(self.middle_section)
        self.num_batteries_select.setFont(font)
        self.num_batteries_select.setMinimum(3)
        self.num_batteries_select.setMaximum(6)
        self.middle_layout.addWidget(self.num_batteries_select, 0, 7, 1, 1)

        self.battery_widget_1 = QtWidgets.QWidget(self.middle_section)
        self.battery_layout_1 = QtWidgets.QVBoxLayout(self.battery_widget_1)
        self.battery_layout_1.setContentsMargins(3, 3, 3, 3)
        self.battery_layout_1.setSpacing(20)

        self.total_1 = QtWidgets.QLabel(self.battery_widget_1)
        self.total_1.setMinimumSize(QtCore.QSize(186, 25))
        self.total_1.setFont(font)
        self.battery_layout_1.addWidget(self.total_1)

        self.info_widget_1 = QtWidgets.QWidget(self.battery_widget_1)
        self.info_layout_1 = QtWidgets.QVBoxLayout(self.info_widget_1)
        self.info_layout_1.setContentsMargins(0, 0, 0, 0)

        self.core_fee_1 = QtWidgets.QLabel(self.info_widget_1)
        self.core_fee_1.setMinimumSize(QtCore.QSize(186, 25))
        self.core_fee_1.setFont(font)
        self.info_layout_1.addWidget(self.core_fee_1)

        self.tax_1 = QtWidgets.QLabel(self.info_widget_1)
        self.tax_1.setMinimumSize(QtCore.QSize(186, 25))
        self.tax_1.setFont(font)
        self.info_layout_1.addWidget(self.tax_1)

        self.battery_layout_1.addWidget(self.info_widget_1)
        self.middle_layout.addWidget(self.battery_widget_1, 1, 0, 1, 3)

        self.line_1 = QtWidgets.QFrame(self.middle_section)
        self.line_1.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_1.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.middle_layout.addWidget(self.line_1, 1, 3, 1, 1)

        self.battery_widget_2 = QtWidgets.QWidget(self.middle_section)
        self.battery_layout_2 = QtWidgets.QVBoxLayout(self.battery_widget_2)
        self.battery_layout_2.setContentsMargins(3, 3, 3, 3)
        self.battery_layout_2.setSpacing(20)

        self.total_2 = QtWidgets.QLabel(self.battery_widget_2)
        self.total_2.setMinimumSize(QtCore.QSize(186, 25))
        self.total_2.setFont(font)
        self.battery_layout_2.addWidget(self.total_2)

        self.info_widget_2 = QtWidgets.QWidget(self.battery_widget_2)
        self.info_layout_2 = QtWidgets.QVBoxLayout(self.info_widget_2)
        self.info_layout_2.setContentsMargins(0, 0, 0, 0)

        self.core_fee_2 = QtWidgets.QLabel(self.info_widget_2)
        self.core_fee_2.setMinimumSize(QtCore.QSize(186, 25))
        self.core_fee_2.setFont(font)
        self.info_layout_2.addWidget(self.core_fee_2)

        self.tax_2 = QtWidgets.QLabel(self.info_widget_2)
        self.tax_2.setMinimumSize(QtCore.QSize(186, 25))
        self.tax_2.setFont(font)
        self.info_layout_2.addWidget(self.tax_2)

        self.battery_layout_2.addWidget(self.info_widget_2)
        self.middle_layout.addWidget(self.battery_widget_2, 1, 4, 1, 1)

        self.line_2 = QtWidgets.QFrame(self.middle_section)
        self.line_2.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.middle_layout.addWidget(self.line_2, 1, 5, 1, 1)

        self.battery_widget_3 = QtWidgets.QWidget(self.middle_section)
        self.battery_layout_3 = QtWidgets.QVBoxLayout(self.battery_widget_3)
        self.battery_layout_3.setContentsMargins(3, 3, 3, 3)
        self.battery_layout_3.setSpacing(20)

        self.total_3 = QtWidgets.QLabel(self.battery_widget_3)
        self.total_3.setMinimumSize(QtCore.QSize(186, 25))
        self.total_3.setFont(font)
        self.battery_layout_3.addWidget(self.total_3)

        self.info_widget_3 = QtWidgets.QWidget(self.battery_widget_3)
        self.info_layout_3 = QtWidgets.QVBoxLayout(self.info_widget_3)
        self.info_layout_3.setContentsMargins(0, 0, 0, 0)

        self.core_fee_3 = QtWidgets.QLabel(self.info_widget_3)
        self.core_fee_3.setMinimumSize(QtCore.QSize(186, 25))
        self.core_fee_3.setFont(font)
        self.info_layout_3.addWidget(self.core_fee_3)

        self.tax_3 = QtWidgets.QLabel(self.info_widget_3)
        self.tax_3.setMinimumSize(QtCore.QSize(186, 25))
        self.tax_3.setFont(font)
        self.info_layout_3.addWidget(self.tax_3)

        self.battery_layout_3.addWidget(self.info_widget_3)
        self.middle_layout.addWidget(self.battery_widget_3, 1, 6, 1, 2)

        self.middle_layout.setColumnStretch(2, 1)
        self.middle_layout.setColumnStretch(4, 1)
        self.middle_layout.setColumnStretch(6, 1)
        self.middle_layout.setRowStretch(1, 1)
        self.window_layout.addWidget(self.middle_section, 1, 0, 1, 2)

        font = QtGui.QFont()
        font.setPointSize(14)
        self.core_fee_check = QtWidgets.QCheckBox(self.central_widget)
        self.core_fee_check.setFont(font)
        self.core_fee_check.setChecked(True)
        self.window_layout.addWidget(self.core_fee_check, 2, 0, 1, 1)

        self.calculate_button = QtWidgets.QPushButton(self.central_widget)
        self.calculate_button.setMinimumSize(QtCore.QSize(111, 31))
        self.calculate_button.setMaximumSize(QtCore.QSize(111, 35))
        self.calculate_button.setFont(font)
        self.calculate_button.clicked.connect(self.calculate_price)
        self.window_layout.addWidget(self.calculate_button, 2, 1, 1, 1)

        self.window_layout.setRowStretch(1, 1)
        self.central_layout.addLayout(self.window_layout, 0, 0, 1, 1)

        self.menubar = MenuBar(self)

        self.setCentralWidget(self.central_widget)

        self.retranslate_ui(self)

        self.price_entry.returnPressed.connect(self.calculate_price)

        self.setMinimumSize(QtCore.QSize())
        self.setMaximumSize(QtCore.QSize())

        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslate_ui(self, main_window):
        main_window.setWindowTitle("Quote Calculator")

        self.battery_quote_label.setText("Batteries")

        self.price_label.setText("Price:")
        self.num_batteries_label.setText("# of batteries:")

        self.total_1.setText("1 Battery: 0.00")
        self.core_fee_1.setText("Core Fee: 0.00")
        self.tax_1.setText("Tax: 0.00")

        self.total_2.setText("2 Batteries: 0.00")
        self.core_fee_2.setText("Core Fee: 0.00")
        self.tax_2.setText("Tax: 0.00")

        self.total_3.setText("3 Batteries: 0.00")
        self.core_fee_3.setText("Core Fee: 0.00")
        self.tax_3.setText("Tax: 0.00")

        self.core_fee_check.setText("Core Fee")
        self.calculate_button.setText("Calculate")

    def calculate_price(self):
        try:
            price = float(self.price_entry.text())
        except ValueError:
            return
        battery_amount = int(self.num_batteries_select.text())
        core_fee = 15 if self.core_fee_check.isChecked() else 0
        tax_rate = get_tax_rate('roseville ca')
        tax = (price + core_fee) * tax_rate
        subtotal = price + core_fee
        total = subtotal + tax

        self.total_1.setText("1 Battery: {:.2f}".format(total))
        self.total_2.setText("2 Batteries: {:.2f}".format(total*2))
        self.total_3.setText("{} Batteries: {:.2f}".format(battery_amount, total*battery_amount))

        self.core_fee_1.setText("Core Fee: {:.2f}".format(core_fee))
        self.core_fee_2.setText("Core Fee: {:.2f}".format(core_fee*2))
        self.core_fee_3.setText("Core Fee: {:.2f}".format(core_fee*battery_amount))

        self.tax_1.setText("Tax: {:.2f}".format(tax))
        self.tax_2.setText("Tax: {:.2f}".format(tax*2))
        self.tax_3.setText("Tax: {:.2f}".format(tax*battery_amount))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ui = BatteryWindow()
    ui.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I should also add that I am using pyinstaller to test my application on other systems. I haven't run the source code on another system due to testing for dependencies. – Cole Abrusci Aug 21 '20 at 03:31

1 Answers1

0

Add this piece of code

    if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
        PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)

    if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
        PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)

inside if __name__ == "__main__": Section if your widgets cut off and squeezed together.

  • The `if __name__ == "__main__":` is only there for testing each individual window. Could I add that to the `__init__` method in my class instead? – Cole Abrusci Aug 21 '20 at 16:50