-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
96 lines (85 loc) · 3.36 KB
/
main.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
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout
from PyQt5.QtWidgets import QPushButton, QLineEdit, QSizePolicy
from PyQt5.QtCore import Qt
class Calculadora(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Calculadora Python')
# self.setFixedSize(430, 550)
self.cw = QWidget()
self.grid = QGridLayout(self.cw)
self.display = QLineEdit()
self.grid.addWidget(self.display, 0, 0, 1, 4)
self.display.setDisabled(True)
self.display.setAlignment(Qt.AlignRight)
self.display.toolTip()
self.display.setStyleSheet(
'* {font-size: 16pt; color: black; font-weight: 300; background: white;}'
)
self.setStyleSheet(
'* {font-size: 12pt; font-weight: 600}'
)
self.display.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
self.add_btn(
QPushButton('C'), 1, 0, 1, 1, lambda: self.display.setText(''),
'background: red;'
)
self.add_btn(
QPushButton('<-'), 1, 1, 1, 1, lambda: self.display.setText(self.display.text()[:-1])
)
self.add_btn(QPushButton('%'), 1, 2, 1, 1)
self.add_btn(QPushButton('/'), 1, 3, 1, 1)
self.add_btn(QPushButton('7'), 2, 0, 1, 1)
self.add_btn(QPushButton('8'), 2, 1, 1, 1)
self.add_btn(QPushButton('9'), 2, 2, 1, 1)
self.add_btn(QPushButton('*'), 2, 3, 1, 1)
self.add_btn(QPushButton('4'), 3, 0, 1, 1)
self.add_btn(QPushButton('5'), 3, 1, 1, 1)
self.add_btn(QPushButton('6'), 3, 2, 1, 1)
self.add_btn(QPushButton('-'), 3, 3, 1, 1)
self.add_btn(QPushButton('1'), 4, 0, 1, 1)
self.add_btn(QPushButton('2'), 4, 1, 1, 1)
self.add_btn(QPushButton('3'), 4, 2, 1, 1)
self.add_btn(QPushButton('+'), 4, 3, 1, 1)
self.add_btn(QPushButton('()'), 5, 0, 1, 1, self.btn_brac)
self.add_btn(QPushButton('0'), 5, 1, 1, 1)
self.add_btn(QPushButton('.'), 5, 2, 1, 1)
self.add_btn(
QPushButton('=='), 5, 3, 1, 1,
self.btn_igual,
'background: darkgreen;'
)
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)
btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
if style:
btn.setStyleSheet(style)
def btn_igual(self):
try:
self.display.setText(
str(eval(self.display.text()))
)
except ZeroDivisionError:
self.display.setText("Não pode dividir por Zero!")
except SyntaxError:
self.display.setText("Expressão Inválida!!!")
def btn_brac(self):
if self.display.text().__contains__('('):
self.display.setText(self.display.text() + ')')
else:
self.display.setText(self.display.text() + '(')
if __name__ == '__main__':
qt = QApplication(sys.argv)
calc = Calculadora()
calc.show()
qt.exec_()