diff --git a/calculator/MainWindow.py b/calculator/MainWindow.py index 234c81c..efcd2eb 100644 --- a/calculator/MainWindow.py +++ b/calculator/MainWindow.py @@ -244,8 +244,9 @@ def setupUi(self, MainWindow): self.pushButton_eq.setStyleSheet("QPushButton {\n" "color: #4CAF50;\n" "}") + self.pushButton_eq.setObjectName("pushButton_eq") - self.gridLayout.addWidget(self.pushButton_eq, 5, 2, 1, 2) + self.gridLayout.addWidget(self.pushButton_eq, 5, 3, 1, 1) self.verticalLayout.addLayout(self.gridLayout) self.verticalLayout_2.addLayout(self.verticalLayout) MainWindow.setCentralWidget(self.centralWidget) @@ -266,9 +267,24 @@ def setupUi(self, MainWindow): self.menuFile.addAction(self.actionExit) self.menuBar.addAction(self.menuFile.menuAction()) + # 在setupUi方法中添加以下代码,创建小数点按钮 + self.pushButton_dot = QtWidgets.QPushButton(self.centralWidget) + self.pushButton_dot.setMinimumSize(QtCore.QSize(0, 50)) + font = QtGui.QFont() + font.setPointSize(27) + font.setBold(True) + font.setWeight(75) + self.pushButton_dot.setFont(font) + self.pushButton_dot.setStyleSheet("QPushButton {\n" + "color: #1976D2;\n" + "}") + self.pushButton_dot.setObjectName("pushButton_dot") + self.gridLayout.addWidget(self.pushButton_dot, 5, 2, 1, 1) + self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) + def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "Calculon")) @@ -315,4 +331,7 @@ def retranslateUi(self, MainWindow): self.actionExit.setShortcut(_translate("MainWindow", "Ctrl+Q")) self.actionReset.setText(_translate("MainWindow", "Reset")) self.actionReset.setShortcut(_translate("MainWindow", "Ctrl+R")) + # 在retranslateUi方法中添加以下代码,为小数点按钮设置文本 + self.pushButton_dot.setText(_translate("MainWindow", ".")) + self.pushButton_dot.setShortcut(_translate("MainWindow", ".")) diff --git a/calculator/calculator.py b/calculator/calculator.py index 4b7feb0..0044805 100644 --- a/calculator/calculator.py +++ b/calculator/calculator.py @@ -18,9 +18,11 @@ def __init__(self, *args, **kwargs): # Setup numbers. for n in range(0, 10): - getattr(self, 'pushButton_n%s' % n).pressed.connect(lambda v=n: self.input_number(v)) + getattr(self, 'pushButton_n%s' % n).pressed.connect(lambda value=n: self.input_number(value)) + # Setup operations. + self.pushButton_dot.pressed.connect(lambda: self.input_dot()) self.pushButton_add.pressed.connect(lambda: self.operation(operator.add)) self.pushButton_sub.pressed.connect(lambda: self.operation(operator.sub)) self.pushButton_mul.pressed.connect(lambda: self.operation(operator.mul)) @@ -66,7 +68,15 @@ def input_number(self, v): self.state = INPUT self.stack[-1] = v else: - self.stack[-1] = self.stack[-1] * 10 + v + current_input = str(self.stack[-1]) + # 检查当前输入是否已经包含小数点 + if '.' in current_input: + # 如果包含小数点,将新输入的数字添加到小数点后面 + integer_part, decimal_part = current_input.split('.') + self.stack[-1] = float(f"{integer_part}.{decimal_part}{v}") + else: + # 如果没有小数点,将新输入的数字追加到当前输入 + self.stack[-1] = self.stack[-1] * 10 + v self.display() @@ -103,6 +113,17 @@ def equals(self): self.state = READY self.display() + def input_dot(self): + if self.state == READY: + self.state = INPUT + self.stack[-1] = 0 + + # 检查当前输入是否已经包含小数点 + if '.' not in str(self.stack[-1]): + # 如果没有小数点,添加小数点到当前输入 + self.stack[-1] = str(self.stack[-1]) + '.' + self.display() + if __name__ == '__main__': app = QApplication([])