-
Notifications
You must be signed in to change notification settings - Fork 216
/
calculator_gui.py
140 lines (113 loc) · 5.54 KB
/
calculator_gui.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
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
from tkinter import *
import parser
import math
# Parser help us to solve mathematical operation
root = Tk()
root.title('CALCULATOR')
root.geometry('670x450')
root.configure(bg='Blue')
# get the user input and place it in the text field
i = 0
def get_variables(num):
global i
display.insert(i, num)
i += 1
def calculate():
entire_string = display.get()
try:
a = parser.expr(entire_string).compile()
result = eval(a)
clear_all()
display.insert(0, result)
except EXCEPTION:
clear_all()
display.insert(0, "ERROR")
def factorial():
n = int(display.get())
fact = math.factorial(n)
clear_all()
display.insert(0, fact)
# adding functionality
def get_operation(operator):
global i
length = len(operator)
display.insert(i, operator)
i += length
# deleting entire elements on the screen
def clear_all():
display.delete(0, END)
# deleting single element on the screen
def undo():
entire_string = display.get()
if len(entire_string):
new_string = entire_string[:-1]
clear_all()
display.insert(0, new_string)
else:
clear_all()
display.insert(0, "ERROR")
# adding the input field
display = Entry(root, font=('Helvetica', '40'))
display.grid(row=1, columnspan=6, padx=45, pady=35, sticky=W + E)
# adding buttons to calculator
Button(root, text="1", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_variables(1)).grid(row=2, column=0)
Button(root, text="2", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_variables(2)).grid(row=2, column=1)
Button(root, text="3", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_variables(3)).grid(row=2, column=2)
Button(root, text="4", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_variables(4)).grid(row=3, column=0)
Button(root, text="5", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_variables(5)).grid(row=3, column=1)
Button(root, text="6", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_variables(6)).grid(row=3, column=2)
Button(root, text="7", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_variables(7)).grid(row=4, column=0)
Button(root, text="8", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_variables(8)).grid(row=4, column=1)
Button(root, text="9", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_variables(9)).grid(row=4, column=2)
# adding other buttons to the calculator
Button(root, text="AC", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: clear_all()).grid(row=5, column=0)
Button(root, text="0", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_variables(0)).grid(row=5, column=1)
Button(root, text="=", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: calculate()).grid(row=5, column=2)
Button(root, text="+", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_operation("+")).grid(row=2,
column=3)
Button(root, text="-", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_operation("-")).grid(row=3,
column=3)
Button(root, text="*", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_operation("*")).grid(row=4,
column=3)
Button(root, text="/", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_operation("/")).grid(row=5,
column=3)
# adding new operations
Button(root, text="pi", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_operation("*3.14")).grid(row=2,
column=4)
Button(root, text="%", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_operation("%")).grid(row=3,
column=4)
Button(root, text="(", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_operation("(")).grid(row=4,
column=4)
Button(root, text="exp", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_operation("**")).grid(row=5,
column=4)
Button(root, text=">-", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: undo()).grid(row=2, column=5)
Button(root, text="!", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: factorial()).grid(row=3, column=5)
Button(root, text=")", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_operation(")")).grid(row=4,
column=5)
Button(root, text="^2", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'),
command=lambda: get_operation("**2")).grid(row=5,
column=5)
root.mainloop()