Calculator GUI in Python (With Tkinter)

In this tutorial, we will build a simple calculator using Python with a graphical user interface.

This calculator can perform basic arithmetic operations like addition, subtraction, multiplication, and division.

1. Requirements

Make sure Python is installed on your system.

Download Python: https://www.python.org/downloads/

2. Steps to Run the Project

Step 1: Install Python IDE.

Step 2: Copy and paste the code into your IDE.

Step 3: Run the program.

Step 4: Use buttons to enter numbers and operations.

Step 5: Click '=' to get the result.

3. Python Code

Python
Simple Calculator using Tkinter
import tkinter as tk


def click(value):
    current = entry.get()
    entry.delete(0, tk.END)
    entry.insert(0, current + str(value))


def clear():
    entry.delete(0, tk.END)


def calculate():
    try:
        result = eval(entry.get())
        entry.delete(0, tk.END)
        entry.insert(0, str(result))
    except:
        entry.delete(0, tk.END)
        entry.insert(0, "Error")


root = tk.Tk()
root.title("Calculator")

entry = tk.Entry(root, width=20, font=("Arial", 18), borderwidth=5, relief="ridge")
entry.grid(row=0, column=0, columnspan=4)

buttons = [
    '7','8','9','/',
    '4','5','6','*',
    '1','2','3','-',
    '0','.','=','+'
]

row = 1
col = 0

for button in buttons:
    if button == '=':
        tk.Button(root, text=button, width=5, height=2, command=calculate).grid(row=row, column=col)
    else:
        tk.Button(root, text=button, width=5, height=2, command=lambda b=button: click(b)).grid(row=row, column=col)

    col += 1
    if col > 3:
        col = 0
        row += 1

# Clear button
tk.Button(root, text="C", width=22, height=2, command=clear).grid(row=row, column=0, columnspan=4)

root.mainloop()

4. Output

A calculator window appears with buttons for digits and operations.

Users can perform basic arithmetic calculations easily.

The result is displayed instantly on the screen.

Conclusion

This project helps in understanding GUI design and event handling in Python.

It is a great beginner project to learn Tkinter and basic programming logic.