简易计算器 通过PyQt5使用designer.exe设计UI,完成简易计算器的基本功能。
时间: 2024-05-17 10:16:35 浏览: 101
Python PyQt5实现的简易计算器功能示例
好的,我会尽力回答你的问题。首先,让我们来看一下如何使用designer.exe来设计UI界面。
设计UI界面的步骤如下:
1. 打开designer.exe
2. 创建一个新的窗口,选择QWidget
3. 在窗口中添加 QLabel、QLineEdit、QPushButton等控件
4. 设置控件的属性和布局
5. 添加响应事件,实现计算器的基本功能
下面是一个简单的计算器程序,你可以根据这个程序进行参考:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout
class Calculator(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Calculator')
self.setGeometry(300, 300, 300, 200)
self.label = QLabel('0')
self.line_edit = QLineEdit()
self.button_1 = QPushButton('1')
self.button_2 = QPushButton('2')
self.button_3 = QPushButton('3')
self.button_4 = QPushButton('4')
self.button_5 = QPushButton('5')
self.button_6 = QPushButton('6')
self.button_7 = QPushButton('7')
self.button_8 = QPushButton('8')
self.button_9 = QPushButton('9')
self.button_0 = QPushButton('0')
self.button_add = QPushButton('+')
self.button_sub = QPushButton('-')
self.button_mul = QPushButton('*')
self.button_div = QPushButton('/')
self.button_clr = QPushButton('Clear')
self.button_eq = QPushButton('=')
vbox = QVBoxLayout()
vbox.addWidget(self.label)
vbox.addWidget(self.line_edit)
hbox1 = QHBoxLayout()
hbox1.addWidget(self.button_1)
hbox1.addWidget(self.button_2)
hbox1.addWidget(self.button_3)
hbox1.addWidget(self.button_add)
hbox2 = QHBoxLayout()
hbox2.addWidget(self.button_4)
hbox2.addWidget(self.button_5)
hbox2.addWidget(self.button_6)
hbox2.addWidget(self.button_sub)
hbox3 = QHBoxLayout()
hbox3.addWidget(self.button_7)
hbox3.addWidget(self.button_8)
hbox3.addWidget(self.button_9)
hbox3.addWidget(self.button_mul)
hbox4 = QHBoxLayout()
hbox4.addWidget(self.button_clr)
hbox4.addWidget(self.button_0)
hbox4.addWidget(self.button_eq)
hbox4.addWidget(self.button_div)
vbox.addLayout(hbox1)
vbox.addLayout(hbox2)
vbox.addLayout(hbox3)
vbox.addLayout(hbox4)
self.setLayout(vbox)
self.button_1.clicked.connect(lambda:self.button_click('1'))
self.button_2.clicked.connect(lambda:self.button_click('2'))
self.button_3.clicked.connect(lambda:self.button_click('3'))
self.button_4.clicked.connect(lambda:self.button_click('4'))
self.button_5.clicked.connect(lambda:self.button_click('5'))
self.button_6.clicked.connect(lambda:self.button_click('6'))
self.button_7.clicked.connect(lambda:self.button_click('7'))
self.button_8.clicked.connect(lambda:self.button_click('8'))
self.button_9.clicked.connect(lambda:self.button_click('9'))
self.button_0.clicked.connect(lambda:self.button_click('0'))
self.button_add.clicked.connect(lambda:self.button_click('+'))
self.button_sub.clicked.connect(lambda:self.button_click('-'))
self.button_mul.clicked.connect(lambda:self.button_click('*'))
self.button_div.clicked.connect(lambda:self.button_click('/'))
self.button_clr.clicked.connect(self.button_clear)
self.button_eq.clicked.connect(self.button_equal)
def button_click(self, text):
self.line_edit.setText(self.line_edit.text() + text)
def button_clear(self):
self.line_edit.clear()
def button_equal(self):
try:
result = eval(self.line_edit.text())
self.label.setText(str(result))
except:
self.label.setText('Error')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Calculator()
ex.show()
sys.exit(app.exec_())
```
在这个程序中,我们使用了QLabel、QLineEdit、QPushButton等控件来构建UI界面,并使用了QVBoxLayout和QHBoxLayout来设置控件的布局。我们还使用了信号和槽机制来实现计算器的基本功能。
希望这能对你有所帮助!
阅读全文