Skip to content

Commit d08abe7

Browse files
authored
Merge pull request #2 from hrosicka/redesign
Redesign
2 parents 28a7096 + 468ee61 commit d08abe7

12 files changed

+109
-13
lines changed

SimpleCalculatorPyQt1.py

+109-13
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def __init__(self, *args, **kwargs):
4444
# Set window title and icon
4545
self.setWindowTitle('PyQt Calculator')
4646
self.setWindowIcon(QIcon(calc_icon))
47+
self.setStyleSheet("""QWidget{background-color: #D8D6E6;}
48+
QToolTip {
49+
border: 1px solid darkgrey;
50+
background-color: #0B132B;
51+
border-radius: 10px;
52+
color: white; }""")
4753

4854
self.calculator = Calculator()
4955

@@ -53,7 +59,12 @@ def __init__(self, *args, **kwargs):
5359

5460
self.label = QLabel('0.0')
5561
self.label.setFont(QFont('Arial', 14))
56-
self.label.setStyleSheet("background-color : white; color : darkblue")
62+
self.label.setStyleSheet("""background-color : white;
63+
color: #0B132B;
64+
border-radius: 10px;
65+
border: 1px solid #7F2982;
66+
min-height: 40px;
67+
""")
5768

5869
self.label.setAlignment(QtCore.Qt.AlignRight)
5970

@@ -74,17 +85,35 @@ def __init__(self, *args, **kwargs):
7485
self.textbox1.setFont(QFont('Arial', 12))
7586
self.textbox1.setValidator(validator)
7687
self.textbox1.setAlignment(QtCore.Qt.AlignRight)
88+
self.textbox1.setStyleSheet("""background-color : white;
89+
color: #0B132B;
90+
border-radius: 10px;
91+
border: 1px solid #7F2982;
92+
min-height: 40px;
93+
""")
7794
self.layout.addRow('Number 1:', self.textbox1)
7895

7996
self.textbox2 = QLineEdit(self)
8097
self.textbox2.setToolTip("<b>Please, enter Number 2!</b>")
8198
self.textbox2.setFont(QFont('Arial', 12))
8299
self.textbox2.setValidator(validator)
83100
self.textbox2.setAlignment(QtCore.Qt.AlignRight)
101+
self.textbox2.setStyleSheet("""background-color : white;
102+
color: #0B132B;
103+
border-radius: 10px;
104+
border: 1px solid #7F2982;
105+
min-height: 40px;
106+
""")
84107
self.layout.addRow('Number 2:', self.textbox2)
85108

86109
# Create a text box for displaying calculation history
87110
self.history = QTextEdit()
111+
self.history.setStyleSheet("""background-color : white;
112+
color: #0B132B;
113+
border-radius: 10px;
114+
border: 1px solid #7F2982;
115+
min-height: 40px;
116+
""")
88117
self.layout.addRow('History:', self.history)
89118

90119
# Create a grid layout for arranging buttons
@@ -98,7 +127,14 @@ def __init__(self, *args, **kwargs):
98127

99128
# Set stylesheet for buttons (background color and text color)
100129
for button in buttons:
101-
button.setStyleSheet("background-color: darkblue; color: white")
130+
button.setStyleSheet("""QPushButton {background-color: #0B132B;
131+
color: white;
132+
border-radius: 10px;
133+
padding: 10px 15px;
134+
margin-top: 0px;
135+
outline: 0px;}
136+
QPushButton:hover {background-color: #7F2982 }
137+
""")
102138

103139
# Set tooltips and functionality for each button:
104140
# Sum button calculates the sum and updates display and history
@@ -141,8 +177,6 @@ def __init__(self, *args, **kwargs):
141177
buttons[7].clicked.connect(app.exit)
142178
self.layout_button.addWidget(buttons[7],3,1)
143179

144-
self.setStyleSheet('QToolTip { border: 3px solid darkgrey;}')
145-
146180
self.show()
147181

148182
def save_history(self):
@@ -151,13 +185,25 @@ def save_history(self):
151185
152186
Checks if the history is empty and displays a message box if so.
153187
"""
188+
dirname = os.path.dirname(__file__)
189+
warning = os.path.join(dirname, 'warning.png')
190+
info = os.path.join(dirname, 'info.png')
154191

155192
# Check if history is empty
156193
if not self.history.toPlainText():
157194
messagebox = QMessageBox(QMessageBox.Warning, "Save History",
158195
"History is empty! Cannot save an empty file.",
159196
buttons=QMessageBox.Ok, parent=self)
160-
messagebox.findChild(QPushButton).setStyleSheet("background-color : darkblue; color : white")
197+
messagebox.setIconPixmap(QPixmap(warning))
198+
messagebox.findChild(QPushButton).setStyleSheet("""QPushButton {background-color: #0B132B;
199+
color: white;
200+
border-radius: 10px;
201+
padding: 10px 15px;
202+
margin-top: 0px;
203+
outline: 0px;
204+
min-width: 100px;}
205+
QPushButton:hover {background-color: #7F2982 }
206+
""")
161207
messagebox.exec_()
162208
return # Early return to prevent further execution if history is empty
163209

@@ -174,7 +220,16 @@ def save_history(self):
174220

175221
# Show a success message box
176222
messagebox = QMessageBox(QMessageBox.Information, "Save History", "History successfully saved to file: " + filepath, buttons=QMessageBox.Ok, parent=self)
177-
messagebox.findChild(QPushButton).setStyleSheet("background-color : darkblue; color : white")
223+
messagebox.setIconPixmap(QPixmap(info))
224+
messagebox.findChild(QPushButton).setStyleSheet("""QPushButton {background-color: #0B132B;
225+
color: white;
226+
border-radius: 10px;
227+
padding: 10px 15px;
228+
margin-top: 0px;
229+
outline: 0px;
230+
min-width: 100px;}
231+
QPushButton:hover {background-color: #7F2982 }
232+
""")
178233
messagebox.exec_()
179234

180235

@@ -204,8 +259,18 @@ def calculate(self, operation):
204259
try:
205260
a = float(self.textbox1.text())
206261
b = float(self.textbox2.text())
207-
self.textbox1.setStyleSheet("background-color : white; color : black")
208-
self.textbox2.setStyleSheet("background-color : white; color : black")
262+
self.textbox1.setStyleSheet("""background-color : white;
263+
color: #0B132B;
264+
border-radius: 10px;
265+
border: 1px solid #7F2982;
266+
min-height: 40px;
267+
""")
268+
self.textbox2.setStyleSheet("""background-color : white;
269+
color: #0B132B;
270+
border-radius: 10px;
271+
border: 1px solid #7F2982;
272+
min-height: 40px;
273+
""")
209274

210275
if operation == 'sum':
211276
res = self.calculator.add(a, b)
@@ -226,18 +291,49 @@ def calculate(self, operation):
226291
self.history.setText(str(a) + ope + str(b) + " = " + str(res) + "\n" + self.history.toPlainText())
227292

228293
except ValueError:
229-
self.textbox1.setStyleSheet("background-color : pink; color : black")
230-
self.textbox2.setStyleSheet("background-color : pink; color : black")
294+
self.textbox1.setStyleSheet("""background-color : white;
295+
color: #0B132B;
296+
border-radius: 10px;
297+
border: 4px solid #F7717D;
298+
min-height: 40px;
299+
""")
300+
self.textbox2.setStyleSheet("""background-color : white;
301+
color: #0B132B;
302+
border-radius: 10px;
303+
border: 4px solid #F7717D;
304+
min-height: 40px;
305+
""")
231306
messagebox = QMessageBox(QMessageBox.Information, "Error", "Input can only be a number!", buttons=QMessageBox.Ok, parent=self)
232307
messagebox.setIconPixmap(QPixmap(stop_writing))
233-
messagebox.findChild(QPushButton).setStyleSheet("background-color : darkblue; color : white")
308+
messagebox.findChild(QPushButton).setStyleSheet("""QPushButton {background-color: #0B132B;
309+
color: white;
310+
border-radius: 10px;
311+
padding: 10px 15px;
312+
margin-top: 0px;
313+
outline: 0px;
314+
min-width: 100px;}
315+
QPushButton:hover {background-color: #7F2982 }
316+
""")
234317
messagebox.exec_()
235318

236319
except ZeroDivisionError:
237-
self.textbox2.setStyleSheet("background-color : pink; color : black")
320+
self.textbox2.setStyleSheet("""background-color : white;
321+
color: #0B132B;
322+
border-radius: 10px;
323+
border: 4px solid #F7717D;
324+
min-height: 40px;
325+
""")
238326
messagebox = QMessageBox(QMessageBox.Warning, "Error", "Division by zero is not allowed!", buttons=QMessageBox.Ok, parent=self)
239327
messagebox.setIconPixmap(QPixmap(stop_writing))
240-
messagebox.findChild(QPushButton).setStyleSheet("background-color : darkblue; color : white")
328+
messagebox.findChild(QPushButton).setStyleSheet("""QPushButton {background-color: #0B132B;
329+
color: white;
330+
border-radius: 10px;
331+
padding: 10px 15px;
332+
margin-top: 0px;
333+
outline: 0px;
334+
min-width: 100px;}
335+
QPushButton:hover {background-color: #7F2982 }
336+
""")
241337
messagebox.exec_()
242338

243339
if __name__ == '__main__':

calc_icon.png

-2.51 MB
Loading

doc/ErrorDividedByZero.PNG

16 KB
Loading

doc/ErrorEmptyHistory.png

1.04 KB
Loading

doc/InputError.PNG

14.1 KB
Loading

doc/MainWindow1.PNG

4.25 KB
Loading

doc/MainWindow2.PNG

5.6 KB
Loading

doc/ResultsAndHistory.PNG

766 Bytes
Loading

doc/Tooltip.png

9.5 KB
Loading

info.png

985 Bytes
Loading

stop_writing.png

-697 Bytes
Loading

warning.png

959 Bytes
Loading

0 commit comments

Comments
 (0)