import tkinter as tk
def button_click(number):
"""Handles button clicks for numbers and the decimal point."""
current = entry.get()
entry.delete(0, tk.END)
entry.insert(0, str(current) + str(number))
def button_clear():
"""Clears the entry field."""
entry.delete(0, tk.END)
def button_equal():
"""Evaluates the expression in the entry field."""
try:
expression = entry.get()
result = eval(expression) # Be cautious with eval() - see notes below
entry.delete(0, tk.END)
entry.insert(0, str(result))
except Exception as e:
entry.delete(0, tk.END)
entry.insert(0, "Error")
# --- Main Window Setup ---
window = tk.Tk()
window.title("Simple Calculator")
window.geometry("300x400") # Set window size
# --- Entry Field ---
entry = tk.Entry(window, width=20, borderwidth=5, font=('Arial', 16))
entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# --- Buttons ---
button_1 = tk.Button(window, text="1", padx=20, pady=10, command=lambda: button_click(1))
button_2 = tk.Button(window, text="2", padx=20, pady=10, command=lambda: button_click(2))
button_3 = tk.Button(window, text="3", padx=20, pady=10, command=lambda: button_click(3))
button_4 = tk.Button(window, text="4", padx=20, pady=10, command=lambda: button_click(4))
button_5 = tk.Button(window, text="5", padx=20, pady=10, command=lambda: button_click(5))
button_6 = tk.Button(window, text="6", padx=20, pady=10, command=lambda: button_click(6))
button_7 = tk.Button(window, text="7", padx=20, pady=10, command=lambda: button_click(7))
button_8 = tk.Button(window, text="8", padx=20, pady=10, command=lambda: button_click(8))
button_9 = tk.Button(window, text="9", padx=20, pady=10, command=lambda: button_click(9))
button_0 = tk.Button(window, text="0", padx=20, pady=10, command=lambda: button_click(0))
button_decimal = tk.Button(window, text=".", padx=20, pady=10, command=lambda: button_click("."))
button_add = tk.Button(window, text="+", padx=20, pady=10, command=lambda: button_click("+"))
button_subtract = tk.Button(window, text="-", padx=20, pady=10, command=lambda: button_click("-"))
button_multiply = tk.Button(window, text="*", padx=20, pady=10, command=lambda: button_click("*"))
button_divide = tk.Button(window, text="/", padx=20, pady=10, command=lambda: button_click("/"))
button_equal = tk.Button(window, text="=", padx=20, pady=10, command=button_equal)
button_clear = tk.Button(window, text="Clear", padx=20, pady=10, command=button_clear)
# --- Button Grid Layout ---
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_0.grid(row=4, column=0)
button_decimal.grid(row=4, column=1)
button_add.grid(row=1, column=3)
button_subtract.grid(row=2, column=3)
button_multiply.grid(row=3, column=3)
button_divide.grid(row=4, column=3)
button_equal.grid(row=5, column=2, columnspan=2)
button_clear.grid(row=5, column=0, columnspan=2)
window.mainloop()
How to Run:
Save the Code: Save the code as a .py file (e.g., calculator.py).
Run: Use the same method as before (Command Prompt/PowerShell or IDLE).
Explanation:
Import: Imports the necessary GUI library.
Functions:
button_click(number): Appends the clicked number to the entry field.
button_clear(): Clears the entry field.
button_equal(): Evaluates the expression in the entry field using eval(). Important Note about
GUI Elements: Creates the window, entry field, and buttons.
Button Commands: Each button's command is set to a function that handles the button click. lambda is used to pass the button's value to the button_click function.
Grid Layout: The grid() method is used to arrange the buttons in a grid layout.
: Starts the Tkinter event loop.
Important Security Note about
The eval() function is powerful but can be dangerous if used with untrusted input. It executes arbitrary Python code. In this simple calculator, it's relatively safe because the user is expected to enter mathematical expressions. However, in a more complex application, you should never use eval() with input from an external source. Consider using a safer alternative like ast.literal_eval() or a dedicated expression parsing library.