Skip to content

Commit 5945e3d

Browse files
committed
Tkinter-Calculator
1 parent 951695a commit 5945e3d

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed
Loading
Loading
Binary file not shown.
Loading
Loading

Projects/tkinter-calculator/main.py

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import tkinter as tk
2+
3+
calculation = ""
4+
5+
6+
def add_to_calculation(symbol):
7+
global calculation
8+
calculation += str(symbol)
9+
# now we have to clear the text field
10+
text_field.delete(1.0, "end")
11+
# later we will insert the results
12+
text_field.insert(1.0, calculation)
13+
14+
15+
def evaluate():
16+
global calculation
17+
try:
18+
calculation = str(eval(calculation))
19+
text_field.delete(1.0, "end")
20+
text_field.insert(1.0, calculation)
21+
except:
22+
clear_field()
23+
text_field.insert(1.0, "ERROR")
24+
25+
26+
def clear_field():
27+
global calculation
28+
calculation = ""
29+
text_field.delete(1.0, "end")
30+
31+
32+
# ------------------------------- UI -------------------------------------------------- #
33+
34+
window = tk.Tk()
35+
window.geometry("340x370")
36+
window.title("My Calculator")
37+
window.config(background="black", highlightthickness=0)
38+
text_field = tk.Text(window, height=2, width=20, font=("Helvetica", 24), background="black", foreground="white", highlightthickness=0)
39+
text_field.grid(columnspan=5)
40+
41+
# button - AC
42+
btn_ac = tk.Button(window, text="AC", command=lambda: clear_field(), width=5, font=("Helvetica", 14), background="#5f5f5f", foreground="black")
43+
btn_ac.grid(row=1, column=0,padx=5, pady=10)
44+
# button brackets
45+
btn_bracket1 = tk.Button(window, text="(", command=lambda: add_to_calculation('('), width=5, font=("Helvetica", 14), background="#5f5f5f", foreground="black")
46+
btn_bracket1.grid(row=1, column=1)
47+
48+
btn_bracket2 = tk.Button(window, text=")", command=lambda: add_to_calculation(')'), width=5, font=("Helvetica", 14), background="#5f5f5f", foreground="black")
49+
btn_bracket2.grid(row=1, column=2)
50+
51+
# division
52+
btn_div = tk.Button(window, text="÷", command=lambda: add_to_calculation('/'), width=5, font=("Helvetica", 14), background="#FFA700", foreground="white")
53+
btn_div.grid(row=1, column=3)
54+
55+
# buttons 7 - 9
56+
btn_7 = tk.Button(window, text="7", command=lambda: add_to_calculation(7), width=5, font=("Helvetica", 14), background="#A9A9A9", foreground="white")
57+
btn_7.grid(row=2, column=0, padx=5, pady=10)
58+
59+
btn_8 = tk.Button(window, text="8", command=lambda: add_to_calculation(8), width=5, font=("Helvetica", 14), background="#A9A9A9", foreground="white")
60+
btn_8.grid(row=2, column=1)
61+
62+
btn_9 = tk.Button(window, text="9", command=lambda: add_to_calculation(9), width=5, font=("Helvetica", 14), background="#A9A9A9", foreground="white")
63+
btn_9.grid(row=2, column=2)
64+
65+
# multiplication
66+
btn_mul = tk.Button(window, text="x", command=lambda: add_to_calculation('*'), width=5, font=("Helvetica", 14), background="#FFA700", foreground="white")
67+
btn_mul.grid(row=2, column=3)
68+
69+
# buttons 4 - 6
70+
btn_4 = tk.Button(window, text="4", command=lambda: add_to_calculation(4), width=5, font=("Helvetica", 14), background="#A9A9A9", foreground="white")
71+
btn_4.grid(row=3, column=0, padx=5, pady=10)
72+
73+
btn_5 = tk.Button(window, text="5", command=lambda: add_to_calculation(5), width=5, font=("Helvetica", 14), background="#A9A9A9", foreground="white")
74+
btn_5.grid(row=3, column=1)
75+
76+
btn_6 = tk.Button(window, text="6", command=lambda: add_to_calculation(6), width=5, font=("Helvetica", 14), background="#A9A9A9", foreground="white")
77+
btn_6.grid(row=3, column=2)
78+
79+
# subtraction
80+
btn_sub = tk.Button(window, text="-", command=lambda: add_to_calculation('-'), width=5, font=("Helvetica", 14), background="#FFA700", foreground="white")
81+
btn_sub.grid(row=3, column=3)
82+
83+
# buttons 1 to 3
84+
btn_1 = tk.Button(window, text="1", command=lambda: add_to_calculation(1), width=5, font=("Helvetica", 14), background="#A9A9A9", foreground="white")
85+
btn_1.grid(row=4, column=0, padx=5, pady=10)
86+
87+
btn_2 = tk.Button(window, text="2", command=lambda: add_to_calculation(2), width=5, font=("Helvetica", 14), background="#A9A9A9", foreground="white")
88+
btn_2.grid(row=4, column=1)
89+
90+
btn_3 = tk.Button(window, text="3", command=lambda: add_to_calculation(3), width=5, font=("Helvetica", 14), background="#A9A9A9", foreground="white")
91+
btn_3.grid(row=4, column=2)
92+
93+
btn_add = tk.Button(window, text="+", command=lambda: add_to_calculation('+'), width=5, font=("Helvetica", 14), background="#FFA700", foreground="white")
94+
btn_add.grid(row=4, column=3)
95+
96+
# zero button
97+
btn_0 = tk.Button(window, text="0", command=lambda: add_to_calculation(0), width=5, font=("Helvetica", 14), background="#A9A9A9", foreground="white")
98+
btn_0.grid(row=5, column=0, padx=5, pady=5)
99+
100+
# mod button
101+
btn_mod = tk.Button(window, text="%", command=lambda: add_to_calculation('%'), width=5, font=("Helvetica", 14), background="#A9A9A9", foreground="white")
102+
btn_mod.grid(row=5, column=1)
103+
104+
# equal button
105+
btn_equal = tk.Button(window, text="=", command=lambda: evaluate(), width=13, font=("Helvetica", 14), background="#FFA700", foreground="white")
106+
btn_equal.grid(row=5, column=2, columnspan=2)
107+
108+
window.mainloop()

0 commit comments

Comments
 (0)