-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
92 lines (66 loc) · 2.71 KB
/
calculator.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
import tkinter as tk
from tkinter import font
def on_button_click(value):
current_text = entry.get()
entry.delete(0, tk.END)
entry.insert(tk.END, current_text + value)
def clear_entry():
entry.delete(0, tk.END)
# def clear_window():
# screen.delete(0,tk.END)
def calculate():
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")
# Create the main window
window = tk.Tk()
window.title("Calculator")
window.geometry("400x550") #width x height
window.resizable(False, False)
window.configure(bg="grey")
# Entry widget to display input and results
entry_font = font.Font(family="Lucida Calligraphy", size=20)
entry = tk.Entry(window, width=14, font=entry_font, justify="right", bd=10, insertwidth=4, bg="#d9d9d9")
entry.grid(row=0, column=0, columnspan=4, pady=20)
# Buttons for digits and operations
# Clear button
button_font = font.Font(family="Lucida Calligraphy", size=18)
row_val = 1
col_val = 0
tk.Button(window, text="C", padx=20, pady=20, font=button_font, command=clear_entry, bg="sky blue", bd=5).grid(row=row_val, column=col_val, sticky="nsew")
buttons = [
'9', '8', '7', '/',
'6', '5', '4', '*',
'3', '2', '1', '-',
'0', '.', '=', '+'
]
for button in buttons:
tk.Button(window, text=button, padx=20, pady=20, font=button_font,
command=lambda btn=button: on_button_click(btn) if btn != '=' else calculate(),
bg="orange", bd=5).grid(row=row_val, column=col_val, sticky="nsew")
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
# for button in buttons:
# tk.Button(window, text=button, padx=20, pady=20, font=button_font,
# command=lambda btn=button: on_button_click(btn) if btn != '=' else calculate(),
# bg="orange", bd=5).grid(row=row_val, column=col_val, sticky="nsew")
# col_val += 1
# if col_val > 3:
# col_val = 0
# row_val += 1
# # Clear button
# tk.Button(window, text="C", padx=20, pady=20, font=button_font, command=clear_entry, bg="sky blue", bd=5).grid(row=row_val, column=col_val, sticky="nsew")
# tk.Button(window, text="AC",padx=30, pady=30, font=button_font, command=clear_window,bg="sky blue", bd=6).pack(row=row_val,column=col_val, sticky="nsew")
# Configure row and column weights
for i in range(1, 6):
window.grid_rowconfigure(i, weight=1)
window.grid_columnconfigure(i-1, weight=1)
# Run the main loop
window.mainloop()
# ..........................AUTHOR ---> GYAN RANJAN..............................