@@ -44,6 +44,12 @@ def __init__(self, *args, **kwargs):
44
44
# Set window title and icon
45
45
self .setWindowTitle ('PyQt Calculator' )
46
46
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; }""" )
47
53
48
54
self .calculator = Calculator ()
49
55
@@ -53,7 +59,12 @@ def __init__(self, *args, **kwargs):
53
59
54
60
self .label = QLabel ('0.0' )
55
61
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
+ """ )
57
68
58
69
self .label .setAlignment (QtCore .Qt .AlignRight )
59
70
@@ -74,17 +85,35 @@ def __init__(self, *args, **kwargs):
74
85
self .textbox1 .setFont (QFont ('Arial' , 12 ))
75
86
self .textbox1 .setValidator (validator )
76
87
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
+ """ )
77
94
self .layout .addRow ('Number 1:' , self .textbox1 )
78
95
79
96
self .textbox2 = QLineEdit (self )
80
97
self .textbox2 .setToolTip ("<b>Please, enter Number 2!</b>" )
81
98
self .textbox2 .setFont (QFont ('Arial' , 12 ))
82
99
self .textbox2 .setValidator (validator )
83
100
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
+ """ )
84
107
self .layout .addRow ('Number 2:' , self .textbox2 )
85
108
86
109
# Create a text box for displaying calculation history
87
110
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
+ """ )
88
117
self .layout .addRow ('History:' , self .history )
89
118
90
119
# Create a grid layout for arranging buttons
@@ -98,7 +127,14 @@ def __init__(self, *args, **kwargs):
98
127
99
128
# Set stylesheet for buttons (background color and text color)
100
129
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
+ """ )
102
138
103
139
# Set tooltips and functionality for each button:
104
140
# Sum button calculates the sum and updates display and history
@@ -141,8 +177,6 @@ def __init__(self, *args, **kwargs):
141
177
buttons [7 ].clicked .connect (app .exit )
142
178
self .layout_button .addWidget (buttons [7 ],3 ,1 )
143
179
144
- self .setStyleSheet ('QToolTip { border: 3px solid darkgrey;}' )
145
-
146
180
self .show ()
147
181
148
182
def save_history (self ):
@@ -151,13 +185,25 @@ def save_history(self):
151
185
152
186
Checks if the history is empty and displays a message box if so.
153
187
"""
188
+ dirname = os .path .dirname (__file__ )
189
+ warning = os .path .join (dirname , 'warning.png' )
190
+ info = os .path .join (dirname , 'info.png' )
154
191
155
192
# Check if history is empty
156
193
if not self .history .toPlainText ():
157
194
messagebox = QMessageBox (QMessageBox .Warning , "Save History" ,
158
195
"History is empty! Cannot save an empty file." ,
159
196
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
+ """ )
161
207
messagebox .exec_ ()
162
208
return # Early return to prevent further execution if history is empty
163
209
@@ -174,7 +220,16 @@ def save_history(self):
174
220
175
221
# Show a success message box
176
222
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
+ """ )
178
233
messagebox .exec_ ()
179
234
180
235
@@ -204,8 +259,18 @@ def calculate(self, operation):
204
259
try :
205
260
a = float (self .textbox1 .text ())
206
261
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
+ """ )
209
274
210
275
if operation == 'sum' :
211
276
res = self .calculator .add (a , b )
@@ -226,18 +291,49 @@ def calculate(self, operation):
226
291
self .history .setText (str (a ) + ope + str (b ) + " = " + str (res ) + "\n " + self .history .toPlainText ())
227
292
228
293
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
+ """ )
231
306
messagebox = QMessageBox (QMessageBox .Information , "Error" , "Input can only be a number!" , buttons = QMessageBox .Ok , parent = self )
232
307
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
+ """ )
234
317
messagebox .exec_ ()
235
318
236
319
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
+ """ )
238
326
messagebox = QMessageBox (QMessageBox .Warning , "Error" , "Division by zero is not allowed!" , buttons = QMessageBox .Ok , parent = self )
239
327
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
+ """ )
241
337
messagebox .exec_ ()
242
338
243
339
if __name__ == '__main__' :
0 commit comments