-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (63 loc) · 1.76 KB
/
main.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
import tkinter as tk
calculation = ""
def addToCalculation(symbol):
global calculation
calculation += str(symbol)
textResult.delete("1.0", "end")
textResult.insert("1.0", calculation)
def evaluateCalculation():
global calculation
try:
calculation = str(eval(calculation))
textResult.delete("1.0", "end")
textResult.insert("1.0", calculation)
except:
clearField()
textResult.insert("1.0", "Error")
def clearField():
global calculation
calculation = ""
textResult.delete("1.0", "end")
# Graphical user interface
root = tk.Tk()
root.geometry("300x430")
root.title("Calculator")
textResult = tk.Text(root, height=2, width=16, font=("Arial", 24))
textResult.grid(columnspan=5, pady=10)
button_config = {
"height": 2,
"width": 3,
"font": ("Arial", 14),
"bd": 0,
"relief": "ridge",
}
# Button symbols and their grid positions
button_symbols = {
"1": (2, 1),
"2": (2, 2),
"3": (2, 3),
"4": (3, 1),
"5": (3, 2),
"6": (3, 3),
"7": (4, 1),
"8": (4, 2),
"9": (4, 3),
"0": (5, 2),
"+": (2, 4),
"-": (3, 4),
"*": (4, 4),
"/": (5, 4),
"(": (5, 1),
")": (5, 3),
}
# Create and place the buttons
for symbol, (row, column) in button_symbols.items():
btn = tk.Button(root, text=symbol, command=lambda symbol=symbol: addToCalculation(symbol), **button_config)
btn.grid(row=row, column=column, padx=5, pady=5)
# Equal button
btn_equals = tk.Button(root, text="=", command=evaluateCalculation, **button_config)
btn_equals.grid(row=6, column=3, columnspan=2)
# Clear button
btn_clear = tk.Button(root, text="C", command=clearField, width=11, font=("Arial", 14), bd=0, relief="ridge")
btn_clear.grid(row=6, column=1, columnspan=2)
root.mainloop()