0

I'm new to pyqt and trying to make a degrees converter application. Now I want to make the text resizable, when I resize MainWindow. The code:


from PyQt6 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(QtWidgets.QMainWindow):
    def resizeEvent(self, event):
        print("Window has been resized")
        QtWidgets.QMainWindow.resizeEvent(self, event)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(305, 250)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(305, 250))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("../../../../Users/Midgetfucker/Downloads/temperature.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setAutoFillBackground(False)
        MainWindow.setDocumentMode(False)
        self.centralwidget = QtWidgets.QWidget(parent=MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_3 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.lineEdit_input = QtWidgets.QLineEdit(parent=self.centralwidget)
        self.lineEdit_input.setStyleSheet("")
        self.lineEdit_input.setLocale(QtCore.QLocale(QtCore.QLocale.Language.Russian, QtCore.QLocale.Country.Russia))
        self.lineEdit_input.setObjectName("lineEdit_input")
        self.gridLayout_3.addWidget(self.lineEdit_input, 0, 1, 1, 1)
        self.label_fahrenheit = QtWidgets.QLabel(parent=self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(8)
        font.setBold(True)
        font.setWeight(75)
        self.label_fahrenheit.setFont(font)
        self.label_fahrenheit.setObjectName("label_fahrenheit")
        self.gridLayout_3.addWidget(self.label_fahrenheit, 0, 0, 1, 1)
        self.plainTextEdit_output = QtWidgets.QPlainTextEdit(parent=self.centralwidget)
        self.plainTextEdit_output.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.CursorShape.ArrowCursor))
        self.plainTextEdit_output.setFocusPolicy(QtCore.Qt.FocusPolicy.NoFocus)
        self.plainTextEdit_output.setObjectName("plainTextEdit_output")
        self.gridLayout_3.addWidget(self.plainTextEdit_output, 1, 0, 1, 2)
        MainWindow.setCentralWidget(self.centralwidget)

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

        self.add_functions()

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Преобразователь температур"))
        self.label_fahrenheit.setText(_translate("MainWindow", "Градусы:"))

    def add_functions(self):
        self.lineEdit_input.textChanged.connect(self.degrees_changed)

    def degrees_changed(self):
        self.plainTextEdit_output.setPlainText("")
        self.plainTextEdit_output.setStyleSheet("")

        degrees = self.lineEdit_input.text()
        if degrees == '':
            return

        degrees = degrees.replace(',', '.')

        try:
            degrees = float(degrees)
        except ValueError:
            self.plainTextEdit_output.setPlainText("Некорректный ввод")
            self.plainTextEdit_output.setStyleSheet("color: rgb(255, 0, 0);")
            self.lineEdit_input.setFocus()
            return

        f_to_c = round((degrees * 9/5 + 32), 2)
        f_to_k = round((degrees - 32) * 5/9 + 273.15, 2)

        c_to_f = round((degrees - 32) * 5/9, 2)
        c_to_k = round(degrees + 273.15, 2)

        k_to_f = round((degrees - 273.15) * 9/5 + 32, 2)
        k_to_c = round(degrees - 273.15, 2)

        if round(f_to_c) == f_to_c:
            f_to_c = int(f_to_c)

        if round(f_to_k) == f_to_k:
            f_to_k = int(f_to_k)

        if round(c_to_f) == c_to_f:
            c_to_f = int(c_to_f)

        if round(c_to_k) == c_to_k:
            c_to_k = int(c_to_k)

        if round(k_to_f) == k_to_f:
            k_to_f = int(k_to_f)

        if round(k_to_c) == k_to_c:
            k_to_c = int(k_to_c)

        if round(degrees) == degrees:
            degrees = int(degrees)

        f_to_c = str(f_to_c)
        f_to_k = str(f_to_k)
        c_to_f = str(c_to_f)
        c_to_k = str(c_to_k)
        k_to_c = str(k_to_c)
        k_to_f = str(k_to_f)
        degrees = str(degrees)

        self.plainTextEdit_output.setPlainText('°F: \n'
                                               + degrees + '°F = ' + f_to_c + '°C' + '\n'
                                               + degrees + '°F = ' + f_to_k + '°K' + '\n\n'
                                               + '°C: \n'
                                               + degrees + '°C = ' + c_to_f + '°F' + '\n'
                                               + degrees + '°C = ' + c_to_k + '°K' + '\n\n'
                                               + '°K: \n'
                                               + degrees + '°K = ' + k_to_f + '°F' + '\n'
                                               + degrees + '°K = ' + k_to_c + '°C')


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

I've already tried this, but doesn't work for me. Any hints or ideas pls?
Windows 10
Using Qt Designer with PyCharm

ploqop
  • 1
  • 1
    Do not try to edit/merge pyuic code, it's a bad practice and can cause unexpected behavior (just like your case: you're never showing your Ui_MainWIndow subclass, so no `resizeEvent` will ever be called). Regenerate that code from the UI, **leave it untouched** and create a QMainWindow subclass that will *use* it, as explained in the official guidelines about [using Designer](//www.riverbankcomputing.com/static/Docs/PyQt6/designer.html). Please note that fonts don't scale very well when changing their size, and you should not try to change font size when resizing, as it might cause recursion. – musicamante Feb 15 '23 at 19:18
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Mario Mateaș Feb 16 '23 at 04:38

0 Answers0