-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculadora.py
93 lines (77 loc) · 3.1 KB
/
calculadora.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
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout
from PyQt5.QtWidgets import QPushButton, QLineEdit, QSizePolicy
class Calculadora(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Calculadora da Fabi')
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: #FFF; 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('C'), 1, 4, 1, 1,
lambda: self.display.setText(''),
'background: #d5580d; color: #FFF; font-weight: 700;'
)
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('<-'), 2, 4, 1, 1,
lambda: self.display.setText(
self.display.text()[:-1]
),
'background: #13823a; color: #fff; font-weight: 700;'
)
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(''), 3, 4, 1, 1)
self.add_btn(QPushButton('.'), 4, 0, 1, 1)
self.add_btn(QPushButton('0'), 4, 1, 1, 1)
self.add_btn(QPushButton(''), 4, 2, 1, 1)
self.add_btn(QPushButton('*'), 4, 3, 1, 1)
self.add_btn(
QPushButton('='), 4, 4, 1, 1,
self.eval_igual,
'background: #095177; color: #fff; font-weight: 700;'
)
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_igual(self):
try:
self.display.setText(
str(eval(self.display.text()))
)
except Exception as e:
self.display.setText('Conta inválida.')
if __name__ == '__main__':
qt = QApplication(sys.argv)
calc = Calculadora()
calc.show()
qt.exec_()