python 程序员计算器实现,使用Qt库实现带有界面操作的
时间: 2024-02-03 09:13:44 浏览: 89
下面是一个使用 PyQt5 实现的简单计算器界面:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLineEdit
class Calculator(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Calculator')
self.setGeometry(300, 300, 300, 200)
# 创建一个文本框
self.text_box = QLineEdit(self)
self.text_box.move(20, 20)
self.text_box.resize(260, 30)
# 创建按钮
self.button_1 = QPushButton('1', self)
self.button_1.move(20, 60)
self.button_1.clicked.connect(lambda: self.button_click('1'))
self.button_2 = QPushButton('2', self)
self.button_2.move(80, 60)
self.button_2.clicked.connect(lambda: self.button_click('2'))
self.button_3 = QPushButton('3', self)
self.button_3.move(140, 60)
self.button_3.clicked.connect(lambda: self.button_click('3'))
self.button_4 = QPushButton('4', self)
self.button_4.move(20, 100)
self.button_4.clicked.connect(lambda: self.button_click('4'))
self.button_5 = QPushButton('5', self)
self.button_5.move(80, 100)
self.button_5.clicked.connect(lambda: self.button_click('5'))
self.button_6 = QPushButton('6', self)
self.button_6.move(140, 100)
self.button_6.clicked.connect(lambda: self.button_click('6'))
self.button_7 = QPushButton('7', self)
self.button_7.move(20, 140)
self.button_7.clicked.connect(lambda: self.button_click('7'))
self.button_8 = QPushButton('8', self)
self.button_8.move(80, 140)
self.button_8.clicked.connect(lambda: self.button_click('8'))
self.button_9 = QPushButton('9', self)
self.button_9.move(140, 140)
self.button_9.clicked.connect(lambda: self.button_click('9'))
self.button_0 = QPushButton('0', self)
self.button_0.move(80, 180)
self.button_0.clicked.connect(lambda: self.button_click('0'))
self.button_add = QPushButton('+', self)
self.button_add.move(200, 60)
self.button_add.clicked.connect(lambda: self.button_click('+'))
self.button_subtract = QPushButton('-', self)
self.button_subtract.move(200, 100)
self.button_subtract.clicked.connect(lambda: self.button_click('-'))
self.button_multiply = QPushButton('*', self)
self.button_multiply.move(200, 140)
self.button_multiply.clicked.connect(lambda: self.button_click('*'))
self.button_divide = QPushButton('/', self)
self.button_divide.move(200, 180)
self.button_divide.clicked.connect(lambda: self.button_click('/'))
self.button_clear = QPushButton('C', self)
self.button_clear.move(20, 180)
self.button_clear.clicked.connect(lambda: self.clear_text_box())
self.button_equal = QPushButton('=', self)
self.button_equal.move(140, 180)
self.button_equal.clicked.connect(lambda: self.calculate())
# 显示界面
self.show()
def button_click(self, text):
"""按钮点击事件"""
self.text_box.setText(self.text_box.text() + text)
def clear_text_box(self):
"""清空文本框"""
self.text_box.setText('')
def calculate(self):
"""计算结果"""
try:
result = eval(self.text_box.text())
self.text_box.setText(str(result))
except Exception as e:
self.text_box.setText('Error')
if __name__ == '__main__':
app = QApplication(sys.argv)
calculator = Calculator()
sys.exit(app.exec_())
```
这个计算器界面包含了数字按钮、加减乘除按钮、等号按钮和清空按钮。用户点击按钮时,按钮的文本会添加到文本框中。当用户点击等号按钮时,程序会尝试使用 `eval()` 函数计算文本框中的表达式,并在文本框中显示结果。如果计算出现异常,程序会在文本框中显示 `Error`。
阅读全文