This repository was archived by the owner on Mar 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
160 lines (121 loc) · 4.28 KB
/
main.py
File metadata and controls
160 lines (121 loc) · 4.28 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from math import floor
from sys import exit
from PySide6.QtCore import Qt
from PySide6.QtGui import QFont
from PySide6.QtWidgets import QApplication, QGridLayout, QLabel, QPushButton, QSizePolicy, QVBoxLayout, QWidget
class ButtonGrid(QWidget):
def __init__(self):
super().__init__()
self.layout = QGridLayout()
positions = [(y, x) for y in range(5) for x in range(4)]
buttons = [ # 4x5 => X = 4, Y = 5 but we need to switch the numbers because of the order of the loop
'C', '/', '*', '-',
'7', '8', '9', '+',
'4', '5', '6', ' ',
'1', '2', '3', '=',
'0', ' ', '.', ' '
]
font = None
# zip returns an interator of tuples based on the two input iterables
for position, text in zip(positions, buttons):
if text == ' ':
continue
button = QPushButton(text)
if font is None:
font = button.font()
font.setPointSizeF(floor(font.pointSizeF() * 1.5))
# Horizontal is Minimum by default, but we need to set Vertical to Minimum too,
# so the button gets taller and doesn't just center between the rows
button.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Minimum)
button.setFont(font)
if text in '+=': # The + and = Buttons need to be 1 cell wide and 2 cells tall
self.layout.addWidget(button, position[0], position[1], 2, 1)
elif text in '0': # The 0 Button needs to be 2 cells wide and 1 cell tall
self.layout.addWidget(button, position[0], position[1], 1, 2)
else: # All other buttons are just 1x1 on the Grid
self.layout.addWidget(button, position[0], position[1])
self.setLayout(self.layout)
class Calculator(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Calculator')
self.setMinimumSize(360, 300)
self.setMaximumSize(360, 500)
self.resize(360, 360)
self.layout = QVBoxLayout()
self.label_font = QFont('nonexistant') # Get the default Monospace Font - https://forum.qt.io/post/208937
self.label_font.setStyleHint(QFont.Monospace)
self.label_font.setPointSizeF(self.label_font.pointSizeF() * 2) # Double the size
self.input_label = QLabel('')
self.input_label.setFont(self.label_font)
self.input_label.setAlignment(Qt.AlignmentFlag.AlignRight)
# For labels we need to set Horizontal to Fixed so that the label only takes up the space that it needs
# Preferred is the default for both Horizontal and Vertical
self.input_label.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Fixed)
self.layout.addWidget(self.input_label)
self.result_label = QLabel('0')
self.result_label.setFont(self.label_font)
self.result_label.setAlignment(Qt.AlignmentFlag.AlignRight)
self.result_label.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Fixed)
self.layout.addWidget(self.result_label)
self.button_grid = ButtonGrid()
self.layout.addWidget(self.button_grid)
self.setLayout(self.layout)
self.input = ''
self.result = 0
self.operator = None
self.errored = False
for button in self.button_grid.findChildren(QPushButton):
button.clicked.connect(self.button_clicked)
def update_labels(self):
self.input_label.setText(str(self.input))
self.result_label.setText(str(self.result))
def button_clicked(self):
label = self.sender().text()
if label == 'C':
self.input = ''
self.result = 0
self.operator = None
self.update_labels()
self.errored = False
if self.errored:
return
if label in '0123456789':
self.input += label
self.update_labels()
elif label in '+-*/':
if self.input:
self.calculate()
self.operator = label
self.input = ''
elif label == '=':
self.calculate()
self.operator = None
self.input = str(self.result)
elif label == '.':
if '.' not in self.input:
self.input += label
self.update_labels()
def calculate(self):
if self.input:
number = float(self.input)
if self.operator == '+':
self.result += number
elif self.operator == '-':
self.result -= number
elif self.operator == '*':
self.result *= number
elif self.operator == '/':
if number != 0:
self.result /= number
else:
self.result = 'Error'
self.errored = True
else:
self.result = number
self.update_labels()
if __name__ == "__main__":
application = QApplication([])
window = Calculator()
window.show()
exit(application.exec())