-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
125 lines (108 loc) · 3.39 KB
/
app.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
119
120
121
122
123
124
125
import sys
from math import pow, sqrt
from PyQt5.QtWidgets import (
QMainWindow,
QApplication,
QPushButton,
QWidget,
QGridLayout,
QLineEdit,
QSizePolicy,
)
COLOR = "color: #00A2FF; font-weight: 700;"
class Calculator(QMainWindow):
def __init__(self, parent=None) -> None:
super().__init__(parent)
self.setWindowTitle("Calculadora")
self.setFixedSize(400, 400)
self.cw = QWidget()
self.grid = QGridLayout(self.cw)
self.display = QLineEdit()
self.grid.addWidget(self.display, 0, 0, 1, 5)
self.display.setDisabled(True)
self.display.setStyleSheet(
"* {background: white; color: #000; font-size: 30px;}"
)
self.display.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
self.add_btn(QPushButton("7"), 1, 0, 1, 1)
self.add_btn(QPushButton("8"), 1, 1, 1, 1)
self.add_btn(QPushButton("9"), 1, 2, 1, 1)
self.add_btn(QPushButton("+"), 1, 3, 1, 1)
self.add_btn(
QPushButton("CE"),
1,
4,
1,
1,
lambda: self.display.setText(""),
COLOR,
)
self.add_btn(QPushButton("4"), 2, 0, 1, 1)
self.add_btn(QPushButton("5"), 2, 1, 1, 1)
self.add_btn(QPushButton("6"), 2, 2, 1, 1)
self.add_btn(QPushButton("-"), 2, 3, 1, 1)
self.add_btn(
QPushButton("C"),
2,
4,
1,
1,
lambda: self.display.setText(self.display.text()[:-1]),
COLOR,
)
self.add_btn(QPushButton("1"), 3, 0, 1, 1)
self.add_btn(QPushButton("2"), 3, 1, 1, 1)
self.add_btn(QPushButton("3"), 3, 2, 1, 1)
self.add_btn(QPushButton("/"), 3, 3, 1, 1)
self.add_btn(
QPushButton("x²"),
3,
4,
1,
1,
lambda: self.display.setText(str(pow(int(self.display.text()), 2))[:-2]),
COLOR
)
self.add_btn(QPushButton("."), 4, 0, 1, 1, None, COLOR)
self.add_btn(QPushButton("0"), 4, 1, 1, 1, None, COLOR)
self.add_btn(
QPushButton("√"),
4,
2,
1,
1,
lambda: self.display.setText(str(sqrt(int(self.display.text())))[:-2]),
COLOR
)
self.add_btn(QPushButton("*"), 4, 3, 1, 1, None, COLOR)
self.add_btn(
QPushButton("="),
4,
4,
1,
1,
self.eval_equal,
COLOR,
)
self.setCentralWidget(self.cw)
def add_btn(self, btn, row, col, rowspan, colspan, funcao=None, style=None):
self.grid.addWidget(btn, row, col, rowspan, colspan)
if not funcao:
btn.clicked.connect(
lambda: self.display.setText(self.display.text() + btn.text())
)
else:
btn.clicked.connect(funcao)
if style:
btn.setStyleSheet(style)
btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
def eval_equal(self):
try:
self.display.setText(str(eval(self.display.text())))
except Exception as e:
self.display.setText("Conta errada")
if __name__ == "__main__":
qt = QApplication(sys.argv)
calc = Calculator()
calc.show()
qt.exec_()