caculator using Applet

import tkinter as tk from tkinter import messagebox def evaluate_expression(): try: result = str(eval(entry_var.get())) entry_var.set(result) except Exception: messagebox.showerror("Error", "Invalid Input") entry_var.set("") def clear_entry(): entry_var.set("") def append_to_entry(value): entry_var.set(entry_var.get() + value) root = tk.Tk() root.title("Simple Calculator") grid_layout = [ ("7", "8", "9", "/"), ("4", "5", "6", "*"), ("1", "2", "3", "-"), ("0", ".", "=", "+"), ("C",) ] entry_var = tk.StringVar() entry = tk.Entry(root, textvariable=entry_var, font=("Arial", 18), justify='right', bd=10, relief=tk.GROOVE) entry.grid(row=0, column=0, columnspan=4, sticky="nsew") for row_index, row in enumerate(grid_layout, start=1): for col_index, button_text in enumerate(row): command = ( evaluate_expression if button_text == "=" else clear_entry if button_text == "C" else lambda text=button_text: append_to_entry(text) ) button = tk.Button(root, text=button_text, font=("Arial", 18), command=command, height=2, width=5) button.grid(row=row_index, column=col_index, sticky="nsew") root.mainloop()

Comments

Popular posts from this blog

BLOG CODE