This repository has been archived by the owner on Nov 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pyqt5_calc.py
118 lines (98 loc) · 3.38 KB
/
pyqt5_calc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget
from functools import partial
import sys
ERROR_MSG = "ERROR"
class PyCalcCtrl:
def __init__(self, model, view):
self._evaluate = model
self._view = view
self._connectSignals()
def _calculateResult(self):
result = self._evaluate(expression=self._view.displayText())
self._view.setDisplayText(result)
def _buildExpression(self, sub_exp):
if self._view.displayText() == ERROR_MSG:
self._view.clearDisplay()
expression = self._view.displayText() + sub_exp
self._view.setDisplayText(expression)
def _connectSignals(self):
for btnText, btn in self._view.buttons.items():
if btnText not in {"=", "C"}:
btn.clicked.connect(partial(self._buildExpression, btnText))
self._view.buttons["="].clicked.connect(self._calculateResult)
self._view.display.returnPressed.connect(self._calculateResult)
self._view.buttons["C"].clicked.connect(self._view.clearDisplay)
class PyCalcUi(QMainWindow):
def setDisplayText(self, text):
self.display.setText(text)
self.display.setFocus()
def displayText(self):
return self.display.text()
def clearDisplay(self):
self.setDisplayText("")
def _createDisplay(self):
self.display = QLineEdit()
self.display.setFixedHeight(35)
self.display.setAlignment(Qt.AlignRight)
self.display.setReadOnly(True)
self.generalLayout.addWidget(self.display)
def _createButtons(self):
self.buttons = {}
buttonsLayout = QGridLayout()
buttons = {
"7": (0, 0),
"8": (0, 1),
"9": (0, 2),
"/": (0, 3),
"C": (0, 4),
"4": (1, 0),
"5": (1, 1),
"6": (1, 2),
"*": (1, 3),
"(": (1, 4),
"1": (2, 0),
"2": (2, 1),
"3": (2, 2),
"-": (2, 3),
")": (2, 4),
"0": (3, 0),
"00": (3, 1),
".": (3, 2),
"+": (3, 3),
"=": (3, 4),
}
for btnText, pos in buttons.items():
self.buttons[btnText] = QPushButton(btnText)
self.buttons[btnText].setFixedSize(40, 40)
buttonsLayout.addWidget(self.buttons[btnText], pos[0], pos[1])
self.generalLayout.addLayout(buttonsLayout)
def __init__(self):
super().__init__()
self.setWindowTitle("PyCalc")
self.setFixedSize(235, 235)
self.generalLayout = QVBoxLayout()
self._centralWidget = QWidget(self)
self.setCentralWidget(self._centralWidget)
self._centralWidget.setLayout(self.generalLayout)
self._createDisplay()
self._createButtons()
def evaluate_expression(expression):
try:
result = str(eval(expression, {}, {}))
except Exception:
result = ERROR_MSG
return result
def main():
pycalc = QApplication(sys.argv)
view = PyCalcUi()
view.show()
model = evaluate_expression
PyCalcCtrl(model=model, view=view)
sys.exit(pycalc.exec_())