-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator_app_midterm.py
More file actions
134 lines (104 loc) · 4.45 KB
/
calculator_app_midterm.py
File metadata and controls
134 lines (104 loc) · 4.45 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
from tkinter import *
# Function to handle button clicks
def button_click(number):
current = displayEntry.get()
# Allowing only one dot in the input
if number == "." and "." in current:
return
displayEntry.delete(0, END)
displayEntry.insert(END, current + str(number))
# Function to handle addition
def button_add():
global first_number
first_number = float(displayEntry.get())
displayEntry.delete(0, END)
operation.set("+")
# Function to handle subtraction
def button_subtract():
global first_number
first_number = float(displayEntry.get())
displayEntry.insert(END,'-')
operation.set("-")
# Function to handle multiplication
def button_multiply():
global first_number
first_number = float(displayEntry.get())
displayEntry.delete(0, END)
operation.set("*")
# Function to handle division
def button_divide():
global first_number
first_number = float(displayEntry.get())
displayEntry.delete(0, END)
operation.set("/")
# Function to handle the equal button click
def button_equal():
second_number = float(displayEntry.get())
displayEntry.delete(END, operation.get())
if operation.get() == "+":
result = first_number + second_number
elif operation.get() == "-":
result = first_number - second_number
elif operation.get() == "*":
result = first_number * second_number
elif operation.get() == "/":
if second_number != 0:
result = first_number / second_number
else:
result = "Error: Division by zero"
displayEntry.insert(0, str(result))
# Create the main window
main = Tk()
main.title("Calculator")
main.geometry('420x370+750+300')
main.resizable(0, 0)
displayEntry = Entry(main, font=('Arial', 12), width=50, border=2, justify="center")
displayEntry.place(x=0, y=0, height=50)
btnHeight = 3
# Row 1
yRow1 = 42
btnClear = Button(text="C", fg='Black', background='White', height=btnHeight, width=36)
btnClear.place(x=0, y=yRow1)
btnPlus = Button(text="+", fg='Black', background='White', height=btnHeight, width=9, command=button_add)
btnPlus.place(x=300, y=yRow1)
# Row 2
yRow2 = 106
btnOne = Button(text="1", fg='Black', background='White', height=btnHeight, width=9, command=lambda: button_click(1))
btnOne.place(x=0, y=yRow2)
btnTwo = Button(text="2", fg='Black', background='White', height=btnHeight, width=9, command=lambda: button_click(2))
btnTwo.place(x=100, y=yRow2)
btnThree = Button(text="3", fg='Black', background='White', height=btnHeight, width=9, command=lambda: button_click(3))
btnThree.place(x=200, y=yRow2)
btnSubtraction = Button(text="-", fg='Black', background='White', height=btnHeight, width=9, command=button_subtract)
btnSubtraction.place(x=300, y=yRow2)
# Row 3
yRow3 = 170
btnFour = Button(text="4", fg='Black', background='White', height=btnHeight, width=9, command=lambda: button_click(4))
btnFour.place(x=0, y=yRow3)
btnFive = Button(text="5", fg='Black', background='White', height=btnHeight, width=9, command=lambda: button_click(5))
btnFive.place(x=100, y=yRow3)
btnSix = Button(text="6", fg='Black', background='White', height=btnHeight, width=9, command=lambda: button_click(6))
btnSix.place(x=200, y=yRow3)
btnMultipy = Button(text="*", fg='Black', background='White', height=btnHeight, width=9, command=button_multiply)
btnMultipy.place(x=300, y=yRow3)
# Row 4
yRow4 = 234
btnSeven = Button(text="7", fg='Black', background='White', height=btnHeight, width=9, command=lambda: button_click(7))
btnSeven.place(x=0, y=yRow4)
btnEigth = Button(text="8", fg='Black', background='White', height=btnHeight, width=9, command=lambda: button_click(8))
btnEigth.place(x=100, y=yRow4)
btnNine = Button(text="9", fg='Black', background='White', height=btnHeight, width=9, command=lambda: button_click(9))
btnNine.place(x=200, y=yRow4)
btnDivision = Button(text="/", fg='Black', background='White', height=btnHeight, width=9, command=button_divide)
btnDivision.place(x=300, y=yRow4)
# Row 5
yRow5 = 298
btnZero = Button(text="0", fg='Black', background='White', height=btnHeight, width=24, command=lambda: button_click(0))
btnZero.place(x=0, y=yRow5)
btnDot = Button(text=".", fg='Black', background='White', height=btnHeight, width=9, command=lambda: button_click('.'))
btnDot.place(x=200, y=yRow5)
btnEqual = Button(text="=", fg='Black', background='White', height=btnHeight, width=9, command=button_equal)
btnEqual.place(x=300, y=yRow5)
# Variable to store the operation
operation = StringVar()
main.mainloop()