Создал GUI в QT Designer. Пытаюсь вставить фнкционал/формулу рассчета индекса массы тела. При запуске программа crashing
Задача: взять данные из двух QSpinBox, при нажатии QPushButton, использовать формулу и вывести результат в lineEdit.
Помогите новичку понять что я делаю не так, код ниже:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(317, 300)
Form.setAutoFillBackground(False)
Form.setStyleSheet("background-color: rgb(211, 244, 255);\n"
"border-color: rgb(43, 43, 43);")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(200, 80, 91, 51))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton.setStyleSheet("QPushButton {\n"
" background-color: rgb(0, 85, 127);\n"
" color: white\n"
"}\n"
" ")
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(30, 80, 111, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(30, 110, 111, 21))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(30, 10, 261, 16))
font = QtGui.QFont()
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(70, 30, 201, 16))
font = QtGui.QFont()
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(Form)
self.label_5.setGeometry(QtCore.QRect(90, 170, 61, 20))
self.label_5.setObjectName("label_5")
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(30, 240, 261, 41))
self.lineEdit_3.setStyleSheet("background-color: rgb(255, 255, 255);")
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setObjectName("lineEdit_3")
self.label_6 = QtWidgets.QLabel(Form)
self.label_6.setGeometry(QtCore.QRect(100, 210, 121, 16))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
self.label_6.setObjectName("label_6")
self.lineEdit_4 = QtWidgets.QLineEdit(Form)
self.lineEdit_4.setGeometry(QtCore.QRect(150, 160, 41, 31))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.lineEdit_4.setFont(font)
self.lineEdit_4.setStyleSheet("background-color: rgb(47, 61, 255);\n"
"color: rgb(255, 255, 255);")
self.lineEdit_4.setObjectName("lineEdit_4")
self.spinBox = QtWidgets.QSpinBox(Form)
self.spinBox.setGeometry(QtCore.QRect(140, 80, 51, 22))
self.spinBox.setMaximum(999)
self.spinBox.setObjectName("spinBox")
self.spinBox.valueChanged.connect(self.result)
self.spinBox_2 = QtWidgets.QSpinBox(Form)
self.spinBox_2.setGeometry(QtCore.QRect(140, 110, 51, 22))
self.spinBox_2.setMaximum(999)
self.spinBox_2.setObjectName("spinBox_2")
self.spinBox_2.valueChanged.connect(self.result)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "BMI Calculator"))
self.pushButton.setText(_translate("Form", "Calculate"))
self.pushButton.clicked.connect(self.result)
self.label.setText(_translate("Form", "Enter your WEIGTH"))
self.label_2.setText(_translate("Form", "Enter your HEIGTH"))
self.label_3.setText(_translate("Form", "To calculate your BMI please enter your data"))
self.label_4.setText(_translate("Form", "below and press Calculate button"))
self.label_5.setText(_translate("Form", "Your BMI is"))
self.label_6.setText(_translate("Form", "Conclusion"))
def result(self):
self.lineEdit_4.setText(self.spinBox.value() / self.spinBox_2.value()) ** 2
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
У вас все сигналы привязаны к одному слоту. Чтобы не было ошибки, надо вставить проверку делителя на ноль.
Не изменяйте код, сгенерированный Qt Designer. Создайте другой класс, который наследуется от соответствующего виджета, и используйте созданный класс для его заполнения.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(317, 300)
Form.setAutoFillBackground(False)
Form.setStyleSheet("background-color: rgb(211, 244, 255);\n"
"border-color: rgb(43, 43, 43);")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(200, 80, 91, 51))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton.setStyleSheet("QPushButton {\n"
" background-color: rgb(0, 85, 127);\n"
" color: white\n"
"}\n"
" ")
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(30, 80, 111, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(30, 110, 111, 21))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(30, 10, 261, 16))
font = QtGui.QFont()
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(70, 30, 201, 16))
font = QtGui.QFont()
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(Form)
self.label_5.setGeometry(QtCore.QRect(90, 170, 61, 20))
self.label_5.setObjectName("label_5")
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(30, 240, 261, 41))
self.lineEdit_3.setStyleSheet("background-color: rgb(255, 255, 255);")
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setObjectName("lineEdit_3")
self.label_6 = QtWidgets.QLabel(Form)
self.label_6.setGeometry(QtCore.QRect(100, 210, 121, 16))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
self.label_6.setObjectName("label_6")
self.lineEdit_4 = QtWidgets.QLineEdit(Form)
self.lineEdit_4.setGeometry(QtCore.QRect(150, 160, 141, 31))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.lineEdit_4.setFont(font)
self.lineEdit_4.setStyleSheet("background-color: rgb(47, 61, 255);\n"
"color: rgb(255, 255, 255);")
self.lineEdit_4.setObjectName("lineEdit_4")
self.spinBox = QtWidgets.QSpinBox(Form)
self.spinBox.setGeometry(QtCore.QRect(140, 80, 51, 22))
self.spinBox.setMaximum(999)
self.spinBox.setObjectName("spinBox")
# self.spinBox.valueChanged.connect(self.result)
self.spinBox_2 = QtWidgets.QSpinBox(Form)
self.spinBox_2.setGeometry(QtCore.QRect(140, 110, 51, 22))
self.spinBox_2.setMaximum(999)
self.spinBox_2.setObjectName("spinBox_2")
# self.spinBox_2.valueChanged.connect(self.result)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "BMI Calculator"))
self.pushButton.setText(_translate("Form", "Calculate"))
# self.pushButton.clicked.connect(self.result)
self.label.setText(_translate("Form", "Enter your WEIGTH"))
self.label_2.setText(_translate("Form", "Enter your HEIGTH"))
self.label_3.setText(_translate("Form", "To calculate your BMI please enter your data"))
self.label_4.setText(_translate("Form", "below and press Calculate button"))
self.label_5.setText(_translate("Form", "Your BMI is"))
self.label_6.setText(_translate("Form", "Conclusion"))
# def result(self):
# self.lineEdit_4.setText(self.spinBox.value() / self.spinBox_2.value()) ** 2
class MenuWindow(QtWidgets.QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.spinBox.valueChanged.connect(self.result)
self.spinBox_2.valueChanged.connect(self.result)
self.pushButton.clicked.connect(self.result)
def result(self):
# self.lineEdit_4.setText(self.spinBox.value() / self.spinBox_2.value()) ** 2
sb_2 = self.spinBox_2.value()
if sb_2:
self.lineEdit_4.setText(f'{(self.spinBox.value() / sb_2) ** 2 : .3f}')
else:
self.lineEdit_4.clear()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# Form = QtWidgets.QWidget()
# ui = Ui_Form()
# ui.setupUi(Form)
# Form.show()
w = MenuWindow()
w.show()
sys.exit(app.exec_())
