-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic_Calculator.py
More file actions
94 lines (84 loc) · 4.28 KB
/
Basic_Calculator.py
File metadata and controls
94 lines (84 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
from tkinter import Tk, Entry, Button, StringVar
class Calculator:
@staticmethod
def evaluate_expression(expr):
try:
# Replace % with /100 for percentage calculation
expr = expr.replace('%', '/100')
allowed_chars = '1234567890.*/%-+() '
expr = ''.join(filter(lambda x: x in allowed_chars, expr))
return eval(expr)
except ValueError:
raise ValueError("Invalid expression")
except ZeroDivisionError:
raise ZeroDivisionError("Division by zero")
except Exception as e:
raise Exception(f"Error: {str(e)}")
def __init__(self, master):
master.title("Calculator")
master.geometry('357x420+0+0')
master.config(bg='gray')
master.resizable(False, False)
self.equation = StringVar()
self.entry_value = ''
Entry(width=17, bg='#ccddff', font=('Arial Bold', 28),
textvariable=self.equation).place(x=0, y=0)
Button(width=11, height=4, text='(', relief='flat', bg='white',
command=lambda: self.show('(')).place(x=0, y=50)
Button(width=11, height=4, text=')', relief='flat', bg='white',
command=lambda: self.show(')')).place(x=90, y=50)
Button(width=11, height=4, text='%', relief='flat', bg='white',
command=lambda: self.show('%')).place(x=180, y=50)
Button(width=11, height=4, text='1', relief='flat', bg='white',
command=lambda: self.show(1)).place(x=0, y=125)
Button(width=11, height=4, text='2', relief='flat', bg='white',
command=lambda: self.show(2)).place(x=90, y=125)
Button(width=11, height=4, text='3', relief='flat', bg='white',
command=lambda: self.show(3)).place(x=180, y=125)
Button(width=11, height=4, text='4', relief='flat', bg='white',
command=lambda: self.show(4)).place(x=0, y=200)
Button(width=11, height=4, text='5', relief='flat', bg='white',
command=lambda: self.show(5)).place(x=90, y=200)
Button(width=11, height=4, text='6', relief='flat', bg='white',
command=lambda: self.show(6)).place(x=180, y=200)
Button(width=11, height=4, text='7', relief='flat', bg='white',
command=lambda: self.show(7)).place(x=0, y=275)
Button(width=11, height=4, text='8', relief='flat', bg='white',
command=lambda: self.show(8)).place(x=180, y=275)
Button(width=11, height=4, text='9', relief='flat', bg='white',
command=lambda: self.show(9)).place(x=90, y=275)
Button(width=11, height=4, text='0', relief='flat', bg='white',
command=lambda: self.show(0)).place(x=90, y=350)
Button(width=11, height=4, text='.', relief='flat', bg='white',
command=lambda: self.show('.')).place(x=180, y=350)
Button(width=11, height=4, text='+', relief='flat', bg='white',
command=lambda: self.show('+')).place(x=270, y=275)
Button(width=11, height=4, text='-', relief='flat', bg='white',
command=lambda: self.show('-')).place(x=270, y=200)
Button(width=11, height=4, text='/', relief='flat', bg='white',
command=lambda: self.show('/')).place(x=270, y=50)
Button(width=11, height=4, text='*', relief='flat', bg='white',
command=lambda: self.show('*')).place(x=270, y=125)
Button(width=11, height=4, text='=', relief='flat',
bg='light yellow', command=self.calculate).place(x=270, y=350)
Button(width=11, height=4, text='C', relief='flat',
bg='white', command=self.clear).place(x=0, y=350)
def show(self, value):
self.entry_value += str(value)
self.equation.set(self.entry_value)
def clear(self):
self.entry_value = ''
self.equation.set(self.entry_value)
def calculate(self):
try:
result = Calculator.evaluate_expression(self.entry_value)
self.equation.set(result)
except ValueError as ve:
self.equation.set(str(ve))
except ZeroDivisionError as zd:
self.equation.set(str(zd))
except Exception as e:
self.equation.set(f"Error: {str(e)}")
root = Tk()
calculator = Calculator(root)
root.mainloop()