Ever wished you had your own calculator tailored just the way you like it? Whether you’re a student, a hobbyist, or just someone who loves Python, building a scientific calculator from scratch is a rewarding project. And guess what? It’s simpler than it sounds.
In this tutorial, I’ll walk you through how to build a sleek, functional scientific calculator using Python and Tkinter. Ready to get nerdy (in a good way)? Let’s dive in!
Why Tkinter + Python?
Python is beginner-friendly and super versatile. Pair it with Tkinter, and you can whip up beautiful GUI applications without breaking a sweat.
What you’ll get:
- A GUI-based calculator with buttons and an entry box
- Support for scientific operations like sine, cosine, logarithms, and square roots
- Constants like π (pi) and e
- A solid understanding of event handling and GUI layout in Tkinter
Prerequisites
Before you start, make sure you have:
- Python installed (preferably 3.6 or higher)
- Basic understanding of functions and event handling
You can install Python from python.org.
Step 1: Import the Required Libraries
import tkinter as tk
from math import *
import re
Here, tkinter is used for the GUI, math provides the scientific functions, and re helps us parse mathematical expressions properly.
Step 2: Set Up the GUI Window
root = tk.Tk()
root.title("Scientific Calculator")
root.geometry("400x600")
Tk()initializes the main window- We set a title and size to make it neat and usable
Step 3: Create the Input Field
entry_var = tk.StringVar()
entry = tk.Entry(root, textvar=entry_var, font=("Arial", 20), bd=10, relief=tk.SUNKEN, justify=tk.RIGHT)
entry.pack(fill=tk.BOTH, ipadx=8, ipady=8, padx=10, pady=10)
- This is where user input and results appear
- Styling helps make it more user-friendly
Step 4: Define Button Click Behavior
def on_click(event):
text = event.widget.cget("text")
current_value = entry_var.get()
if text == "=":
try:
parsed_expression = re.sub(r'\\b(sqrt|log|sin|cos|tan|exp|pi|e)\\b', r'\\1', current_value)
result = eval(parsed_expression, {"sqrt": sqrt, "log": log, "sin": sin, "cos": cos, "tan": tan, "exp": exp, "pi": pi, "e": e})
entry_var.set(result)
except Exception as err:
entry_var.set(f"Error: {str(err)}")
elif text == "C":
entry_var.set("")
else:
entry_var.set(current_value + text)
on_clickcaptures the button text and updates the input field- It uses
eval()to calculate the expression safely using a restricted dictionary
Step 5: Design the Calculator Layout
buttons = [
["7", "8", "9", "/", "sqrt"],
["4", "5", "6", "*", "log"],
["1", "2", "3", "-", "sin"],
["0", ".", "=", "+", "cos"],
["C", "^", "tan", "exp", "("],
[")", "pi", "e", "mod", "%"]
]
Each sublist represents a row of buttons.
Now generate the buttons dynamically:
for row in buttons:
frame = tk.Frame(root)
frame.pack(fill=tk.BOTH, expand=True)
for btn_text in row:
btn = tk.Button(frame, text=btn_text, font=("Arial", 18), relief=tk.GROOVE, bd=3, width=5, height=2)
btn.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
btn.bind("<Button-1>", on_click)
- Buttons are neatly grouped in frames
- Clicking any button triggers the
on_clickfunction
Step 6: Run the Application
Finally, start the GUI loop:
root.mainloop()
This keeps the window open and responsive to user interaction.
Final Thoughts
Congratulations! You’ve just built a fully functioning scientific calculator. With a touch of styling and maybe a splash of color, you can make it even more elegant.

Want to take it to the next level?
- Add keyboard input support
- Introduce themes (dark/light mode)
- Package it into an executable using PyInstaller
Call to Action
Enjoyed building this? Share your version or ask questions in the comments! Want more Python projects like this? Check out our AI Automation Services or dive into more dev tutorials on our blog.