Calcutta
import tkinter as tk
# Function to update the input field
def update_expression(expression):
current_text = input_text.get()
input_text.set(current_text + expression)
# Function to calculate the result
def calculate_result():
try:
result = str(eval(input_text.get()))
input_text.set(result)
except:
input_text.set("Error")
# Function to clear the input field
def clear_input():
input_text.set("")
# Create main window
window = tk.Tk()
window.title("Calculator")
window.geometry("400x600")
window.resizable(0, 0)
window.configure(bg="black")
input_text = tk.StringVar()
# Create input field
input_field = tk.Entry(window, font=('Arial', 24), textvariable=input_text, bd=10, insertwidth=4, width=14, borderwidth=4)
input_field.grid(row=0, column=0, columnspan=4)
# Button style
button_style = {"font": ('Arial', 18), "bg": "white", "fg": "black", "bd": 1, "width": 5, "height": 2, "relief": "flat"}
# Add buttons to the calculator
buttons = [
('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
('0', 4, 0), ('.', 4, 1), ('+', 4, 2), ('=', 4, 3),
]
for (text, row, col) in buttons:
action = lambda x=text: update_expression(x) if x != '=' else calculate_result()
tk.Button(window, text=text, command=action, **button_style).grid(row=row, column=col)
# Clear button
tk.Button(window, text='C', command=clear_input, **button_style, bg="deep pink", fg="white").grid(row=5, column=0, columnspan=4, sticky="nsew")
# Run the window loop
window.mainloop()
0 Comments