PyQt5 简易计算器

剩下计算函数(self.calculator)未实现,有兴趣的朋友可以实现它

 

【知识点】

1、利用循环添加按钮部件,及给每个按钮设置信号/槽

2、给按钮设置固定大小:button.setFixedSize(QtCore.QSize(60,30))

3、取事件的的发送者(此例为各个按钮)的文本: self.sender().text()

 

【效果图】

PyQt5 简易计算器

 

【源代码】

 1 import sys
 2 from PyQt5 import QtWidgets,QtCore,QtGui
 3 
 4 
 5 class Example(QtWidgets.QWidget):
 6 
 7     def __init__(self):
 8         super(Example, self).__init__()
 9         self.initUI()
10         
11         self.reset()
12 
13     def initUI(self):
14         self.setWindowTitle('简易计算器')
15         grid = QtWidgets.QGridLayout()
16         
17         self.display = QtWidgets.QLineEdit('0')
18         self.display.setFont(QtGui.QFont("Times", 20))
19         self.display.setReadOnly(True)
20         self.display.setAlignment(QtCore.Qt.AlignRight)
21         self.display.setMaxLength(15)
22         grid.addWidget(self.display,0,0,1,4)
23 
24         names = ['Clear', 'Back', '', 'Close', 
25                 '7', '8', '9', '/',
26                 '4', '5', '6', '*', 
27                 '1', '2', '3', '-',
28                 '0', '.', '=', '+']
29         pos = [(0, 0), (0, 1), (0, 2), (0, 3),
30                 (1, 0), (1, 1), (1, 2), (1, 3),
31                 (2, 0), (2, 1), (2, 2), (2, 3),
32                 (3, 0), (3, 1), (3, 2), (3, 3 ),
33                 (4, 0), (4, 1), (4, 2), (4, 3)]
34         c = 0
35         for name in names:
36             button = QtWidgets.QPushButton(name)
37             button.setFixedSize(QtCore.QSize(60,30))
38             button.clicked.connect(self.buttonClicked) # 给每个按钮设置信号/槽
39             if c == 2:
40                 pass
41                 #grid.addWidget(QtWidgets.QLabel(''), 0, 2) #替换 第三个按钮 为 文本标签!
42             else: 
43                 grid.addWidget(button, pos[c][0]+1, pos[c][1])
44             c = c + 1
45         
46         self.setLayout(grid)
47         
48         
49         
50         
51     def buttonClicked(self): 
52         #sender = self.sender();  # 确定信号发送者
53         #self.display.setText(sender.text())
54         text = self.sender().text()
55         if text in '+-*/':
56             self.history.append(self.number) # 数字入栈
57             self.history.append(text) # 运算符入栈
58             self.operator = text # 设置当前运算符
59             self.number = "" # 数字清空
60             self.numberType = "int"
61             return
62         elif text == "=":
63             self.calculate() # 计算
64         elif text == "Back":
65             pass
66         elif text == "Clear":
67             self.reset()
68         elif text == "Close":
69             self.close()
70         elif text == ".":
71             if self.numberType == "int":
72                 self.number += text
73                 self.numberType = "float"
74         else:
75             self.number = self.number + text if self.number != "0" else text
76 
77         self.display.setText(self.number)
78         
79     def calculate(self):
80         pass
81         
82     def reset(self):
83         self.number = "0"
84         self.result = 0
85         self.history = []
86         self.operator = '' # +,-,*,/
87         self.numberType = 'int' # int与float两种,如果输入了小数点则为实数
88 
89 app = QtWidgets.QApplication(sys.argv)
90 ex = Example()
91 ex.show()
92 sys.exit(app.exec_())

 

上一篇:PyQt5利用QPainter绘制各种图形


下一篇:【转】QT事件传递与事件过滤器