-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodSoft Task#2.py
More file actions
71 lines (53 loc) · 2.08 KB
/
CodSoft Task#2.py
File metadata and controls
71 lines (53 loc) · 2.08 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
import tkinter as tk
# Function to update the entry field
def button_click(value):
current = str(entry.get())
entry.delete(0, tk.END)
entry.insert(0, current + value)
# Function to clear the entry field
def button_clear():
entry.delete(0, tk.END)
# Function to evaluate and display the result
def button_equal():
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(0, str(result))
except Exception as e:
entry.delete(0, tk.END)
entry.insert(0, "Error")
# Create the main window
root = tk.Tk()
root.title("Calculator")
root.geometry("400x700") # height
root.configure(bg="#0D1B2A") # Set background color
# Create entry field
entry = tk.Entry(root, width=16, font=("Arial", 32), borderwidth=2, relief="solid", bg="#FFFFFF", justify="right") # Set font size
entry.grid(row=0, column=0, columnspan=4, pady=10, padx=10, ipadx=10, sticky='nsew')
# Define button layout
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
row_val = 1
col_val = 0
# Create buttons
for button in buttons:
if button == '=':
tk.Button(root, text=button, padx=40, pady=40, font=("Arial", 20), command=button_equal, bg="#274C77", fg="#FFFFFF").grid(row=row_val, column=col_val, sticky='nsew')
elif button == 'C':
tk.Button(root, text=button, padx=40, pady=40, font=("Arial", 20), command=button_clear, bg="#6096BA", fg="#FFFFFF").grid(row=row_val, column=col_val, sticky='nsew')
else:
tk.Button(root, text=button, padx=40, pady=40, font=("Arial", 20), command=lambda value=button: button_click(value), bg="#6096BA", fg="#FFFFFF").grid(row=row_val, column=col_val, sticky='nsew')
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
# Configure grid to have uniform size
for i in range(5): # Increased the range to accommodate the larger buttons
root.grid_columnconfigure(i, weight=1)
root.grid_rowconfigure(i+1, weight=1)
# Run the application
root.mainloop()