A
A
Artyom2021-02-27 23:47:22
PyQt
Artyom, 2021-02-27 23:47:22

How to assign value from spinBox to text?

I want to do (or rather I already did) the following:
I enter values ​​​​in spinBox1 and spinBox2, then calculate according to the formula P / D * 100%
and how it all works, only I need to assign the value from
spinBox1 and spinBox2 to the text that I have already set . How can I do this? Here is my code:

The code
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'FinanceHelper.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(590, 279)
        MainWindow.setStyleSheet("background-color: rgb(170, 170, 0);")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
        self.tabWidget.setGeometry(QtCore.QRect(0, 0, 601, 291))
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.spinBox = QtWidgets.QSpinBox(self.tab)
        self.spinBox.setGeometry(QtCore.QRect(10, 50, 221, 22))
        self.spinBox.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.spinBox.setMaximum(1000000000)
        self.spinBox.setObjectName("spinBox")
        self.label = QtWidgets.QLabel(self.tab)
        self.label.setGeometry(QtCore.QRect(10, 20, 47, 13))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.tab)
        self.label_2.setGeometry(QtCore.QRect(10, 80, 47, 13))
        self.label_2.setObjectName("label_2")
        self.spinBox_2 = QtWidgets.QSpinBox(self.tab)
        self.spinBox_2.setGeometry(QtCore.QRect(10, 100, 221, 22))
        self.spinBox_2.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.spinBox_2.setObjectName("spinBox_2")
        self.pushButton = QtWidgets.QPushButton(self.tab)
        self.pushButton.setGeometry(QtCore.QRect(10, 130, 221, 41))
        self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton.setStyleSheet("color: rgb(255, 255, 255);\n"
"background-color: rgb(0, 170, 0);")
        self.pushButton.setCheckable(False)
        self.pushButton.setAutoRepeat(False)
        self.pushButton.setAutoExclusive(False)
        self.pushButton.setObjectName("pushButton")
        self.label_3 = QtWidgets.QLabel(self.tab)
        self.label_4 = QtWidgets.QLabel(self.tab)
        self.label_3.setEnabled(True)
        self.label_3.setGeometry(QtCore.QRect(260, 70, 211, 31))
        self.label_3.setAutoFillBackground(False)
        self.label_3.setStyleSheet("color: rgb(85, 0, 0);\n"
"font: 75 20pt \"MS Shell Dlg 2\";")
        self.label_3.setObjectName("label_3")
        self.label_4.setObjectName("label_4")
        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.tabWidget.addTab(self.tab_2, "")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.start_calc()

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "123"))
        self.label.setText(_translate("MainWindow", "1:"))
        self.label_2.setText(_translate("MainWindow", "2:"))
        self.pushButton.setText(_translate("MainWindow", "Расчитать"))
        self.label_3.setText(_translate("MainWindow", "Значение: "))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "45343"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))

    def start_calc(self):
        self.pushButton.clicked.connect(lambda: self.calc(self.spinBox.value(), self.spinBox_2.value()))

    def calc(self, val1, val2):
        if len(self.label_3.text()):
            self.label_3.setText(self.label_3.text() + val1 / val2 * 100)


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


At the moment, an error is thrown: Process finished with exit code -1073740791 (0xC0000409), not the correct type, as I understand it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2021-03-09
@bbkmzzzz

self.label_3.text() + str(val1 / val2 * 100)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question