Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion 21 calculator/MainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"))
Expand Down Expand Up @@ -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", "."))

25 changes: 23 additions & 2 deletions 25 calculator/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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([])
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.