Calculator Using Python

🔢 Build a Mobile Calculator Using Python (with CustomTkinter UI)

If you're learning Python and want to build a simple yet powerful calculator, this project is perfect for you! Today, we’ll create a mobile-style calculator in Python using the customtkinter library — a modern UI framework that makes Python apps look beautiful and user-friendly.

Whether you're searching for:

  • calculator using python
  • calculator code in python
  • python calculator program

...or anything similar, this tutorial will walk you through building your own GUI calculator from scratch!


📦 What You Need to Get Started

Before running the code, make sure you’ve installed the required library. Open your terminal or command prompt and type:

pip install customtkinter

This project uses customtkinter to give your calculator a sleek and mobile-inspired design. You don't need any advanced UI design skills—Python handles everything for you!


🧠 Calculator Program in Python — Source Code

🌐 Calculator Using Python



import customtkinter as ctk
import math

ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")

app = ctk.CTk()
app.geometry("360x640")
app.title("Mobile Calculator")

# === Display Section === 
entry_frame = ctk.CTkFrame(app, fg_color="#000000" , corner_radius=12)
entry_frame.pack(padx=15, pady=20, fill="x")

input_var = ctk.StringVar()
output_var = ctk.StringVar()

input_label = ctk.CTkLabel(entry_frame, textvariable = input_var,
                           font=("segoe UI", 24), anchor= "e",
                           text_color="#ffffff", height=50)
input_label.pack(fill="x", padx=12, pady=(12,4))

output_label = ctk.CTkLabel(entry_frame, textvariable = output_var,
                           font=("segoe UI", 24, "bold"), anchor= "e",
                           text_color="#ffffff", height=60)
output_label.pack(fill="x", padx=12, pady=(0,12))

def press(key):
    if key == "=":
        try:
            expression = input_var.get().replace("^", "**")
            result = eval(expression)
            output_var.set(str(round(result, 6)))
        except ZeroDivisionError:
            output_var.set("Div by 0")
        except:
            output_var.set("Error")
    elif key == "c":
        input_var.set("")
        output_var.set("")
    elif key == "⌫":
        input_var.set(input_var.get()[:-1])
    elif key == "√":
        try:
            value = float(input_var.get())
            output_var.set(str(round(math.sqrt(value), 6)))
        except:
            output_var.set("Error")
    elif key == "log":
        try:
            value = float(input_var.get())
            output_var.set(str(round(math.log10(value), 6)))
        except:
            output_var.set("Error")
    else:
        input_var.set(input_var.get() + key)

# === Buttons ===
btn_frame = ctk.CTkFrame(app, fg_color="#1c1c1c" , corner_radius=0)
btn_frame.pack(padx=10, pady=0 , fill="both" , expand= True)

buttons = [
    ["c", "⌫", "%" , "/"],
    ["7", "8", "9" , "*"],
    ["4","5","6","-"],
    ["1","2","3","+"],
    ["0","." , "^","="]
]

def create_button(txt, row, col):
    color="#FF9500" if txt== "=" else "#2e2e2e"
    button = ctk.CTkButton(
        btn_frame, text=txt , corner_radius=100,
        fg_color=color,
        hover_color="#555555",
        font=("Segoe UI" , 20, "bold"),
        text_color= "#ffffff", 
        command = lambda: press(txt)
    )
    button.grid (row= row, column= col, sticky="nsew", padx=5 ,pady=5)

# Create and place buttons
for r, row_vals in enumerate(buttons):
    for c, char in enumerate(row_vals):
        create_button(char, r, c)

# Equal sizing

for i in range(5):
    btn_frame.rowconfigure(i, weight=1)
for j in range(4):
    btn_frame.columnconfigure(j, weight=1)
app.mainloop()

🔍 Output Preview

✨ What Makes This Python Calculator Special?

This python simple calculator project includes:

  • Mobile-like layout with responsive buttons
  • Dark mode UI with glowing button effects
  • Supports %, ^, √, log, decimal, and all basic operations
  • Error handling (like divide by zero, invalid input)

If you're building a calculator in python or looking for clean calculator python code, this is a great base project to start from.