[PyQt6] PartⅡ. Simple Calculate with Qt Designer
caculate
没什么好说的,只能说英语好,就是无敌。
以后每天背500个单词。
重点代码
self.pushButton_plus.clicked.connect(self.add) # 绑定事件
def add(self):
fnum = int(self.lineEdit_first.text()) #lineEdit.text()获取输入文本框的内容,字符串类型
secnum = int(self.lineEdit_second.text())
result = fnum + secnum
self.label_result.setText(f"Addition: {result}")
def min(self):
fnum = int(self.lineEdit_first.text()) #lineEdit.text()获取输入文本框的内容,字符串类型
secnum = int(self.lineEdit_second.text())
result = fnum - secnum
self.label_result.setText(f"Minus: {result}")
def mul(self):
fnum = int(self.lineEdit_first.text()) #lineEdit.text()获取输入文本框的内容,字符串类型
secnum = int(self.lineEdit_second.text())
result = fnum * secnum
self.label_result.setText(f"Multiply: {result}")
def div(self):
fnum = int(self.lineEdit_first.text()) #lineEdit.text()获取输入文本框的内容,字符串类型
secnum = int(self.lineEdit_second.text())
result = fnum / secnum
self.label_result.setText(f"Divide: {result}")
完整代码
# Form implementation generated from reading ui file 'computer.ui'
#
# Created by: PyQt6 UI code generator 6.3.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(500, 300)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(12)
font.setBold(True)
Form.setFont(font)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(14)
font.setBold(True)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding,
QtWidgets.QSizePolicy.Policy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.lineEdit_first = QtWidgets.QLineEdit(Form)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(12)
font.setBold(True)
self.lineEdit_first.setFont(font)
self.lineEdit_first.setObjectName("lineEdit_first")
self.horizontalLayout.addWidget(self.lineEdit_first)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(14)
font.setBold(True)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding,
QtWidgets.QSizePolicy.Policy.Minimum)
self.horizontalLayout_2.addItem(spacerItem1)
self.lineEdit_second = QtWidgets.QLineEdit(Form)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(12)
font.setBold(True)
self.lineEdit_second.setFont(font)
self.lineEdit_second.setObjectName("lineEdit_second")
self.horizontalLayout_2.addWidget(self.lineEdit_second)
self.verticalLayout.addLayout(self.horizontalLayout_2)
spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum,
QtWidgets.QSizePolicy.Policy.Expanding)
self.verticalLayout.addItem(spacerItem2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.pushButton_plus = QtWidgets.QPushButton(Form)
self.pushButton_plus.setObjectName("pushButton_plus")
self.pushButton_plus.clicked.connect(self.add) # 绑定事件
self.horizontalLayout_3.addWidget(self.pushButton_plus)
self.pushButton_minus = QtWidgets.QPushButton(Form)
self.pushButton_minus.setObjectName("pushButton_minus")
self.pushButton_minus.clicked.connect(self.min) # 绑定事件
self.horizontalLayout_3.addWidget(self.pushButton_minus)
self.pushButton_mul = QtWidgets.QPushButton(Form)
self.pushButton_mul.setObjectName("pushButton_mul")
self.pushButton_mul.clicked.connect(self.mul) # 绑定事件
self.horizontalLayout_3.addWidget(self.pushButton_mul)
self.pushButton_div = QtWidgets.QPushButton(Form)
self.pushButton_div.setObjectName("pushButton_div")
self.pushButton_div.clicked.connect(self.div) # 绑定事件
self.horizontalLayout_3.addWidget(self.pushButton_div)
self.verticalLayout.addLayout(self.horizontalLayout_3)
spacerItem3 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum,
QtWidgets.QSizePolicy.Policy.Expanding)
self.verticalLayout.addItem(spacerItem3)
self.label_result = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(14)
font.setBold(True)
self.label_result.setFont(font)
self.label_result.setStyleSheet("QLabel{\n"
"\n"
" color: green\n"
"}")
self.label_result.setText("")
self.label_result.setObjectName("label_result")
self.verticalLayout.addWidget(self.label_result)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "First Num"))
self.label_2.setText(_translate("Form", "Second Num"))
self.pushButton_plus.setText(_translate("Form", "+"))
self.pushButton_minus.setText(_translate("Form", "-"))
self.pushButton_mul.setText(_translate("Form", "*"))
self.pushButton_div.setText(_translate("Form", "/"))
def add(self):
fnum = int(self.lineEdit_first.text()) #lineEdit.text()获取输入文本框的内容,字符串类型
secnum = int(self.lineEdit_second.text())
result = fnum + secnum
self.label_result.setText(f"Addition: {result}")
def min(self):
fnum = int(self.lineEdit_first.text()) #lineEdit.text()获取输入文本框的内容,字符串类型
secnum = int(self.lineEdit_second.text())
result = fnum - secnum
self.label_result.setText(f"Minus: {result}")
def mul(self):
fnum = int(self.lineEdit_first.text()) #lineEdit.text()获取输入文本框的内容,字符串类型
secnum = int(self.lineEdit_second.text())
result = fnum * secnum
self.label_result.setText(f"Multiply: {result}")
def div(self):
fnum = int(self.lineEdit_first.text()) #lineEdit.text()获取输入文本框的内容,字符串类型
secnum = int(self.lineEdit_second.text())
result = fnum / secnum
self.label_result.setText(f"Divide: {result}")
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())