Untitled
unknown
plain_text
2 years ago
1.6 kB
5
Indexable
import tkinter as tk # Function to handle button clicks def button_click(number): current = display.get() display.delete(0, tk.END) display.insert(tk.END, current + str(number)) # Function to handle equal button click def calculate(): expression = display.get() try: result = eval(expression) display.delete(0, tk.END) display.insert(tk.END, str(result)) except: display.delete(0, tk.END) display.insert(tk.END, "Error") # Function to clear the display def clear(): display.delete(0, tk.END) # Create the main window window = tk.Tk() window.title("Modern Calculator") window.geometry("300x400") window.config(bg="blue") # Create the display display = tk.Entry(window, font=("Arial", 20), bd=0, justify=tk.RIGHT) display.grid(row=0, column=0, columnspan=4, padx=10, pady=10) # Create the number buttons 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 button_text, row, col in buttons: button = tk.Button(window, text=button_text, font=("Arial", 15), bd=0, command=lambda text=button_text: button_click(text)) button.grid(row=row, column=col, padx=5, pady=5) # Create the clear button clear_button = tk.Button(window, text="C", font=("Arial", 15), bd=0, command=clear) clear_button.grid(row=5, column=0, columnspan=4, padx=5, pady=5) # Start the main loop window.mainloop()
Editor is loading...