DEV Community

Cover image for How to Build a Graphical Calculator with Python and Tkinter
Mohammad Taha Heidarifard
Mohammad Taha Heidarifard

Posted on

How to Build a Graphical Calculator with Python and Tkinter

How to Build a Simple Graphical Calculator with Python and Tkinter

If you're learning Python and looking for a small project to practice your skills, building a graphical calculator is a great place to start.

In this tutorial, we'll build a simple calculator using Python and Tkinter. We'll create the graphical interface, add buttons, handle user input, and perform basic mathematical operations.

Why Python and Tkinter?

Python is one of the most beginner-friendly programming languages, while Tkinter provides a simple way to create desktop graphical interfaces.

One advantage of Tkinter is that it comes with the standard Python installation on most systems, so you don't usually need to install an additional GUI framework.

This makes it a good choice for small educational projects.

What We'll Build

Our calculator will include:

  • A display for mathematical expressions
  • Number buttons from 0 to 9
  • Basic arithmetic operators
  • A clear button
  • An equals button
  • A simple graphical layout

The goal isn't to create a production-level calculator, but to understand how a GUI application works in Python.

1. Import Tkinter

First, import Tkinter:

import tkinter as tk
Enter fullscreen mode Exit fullscreen mode

We'll use the tk module to create the application window and interface elements.

2. Create the Main Window

Next, create the main application window:

root = tk.Tk()
root.title("Calculator")
root.geometry("300x400")

root.mainloop()
Enter fullscreen mode Exit fullscreen mode

The Tk() object represents the main window.

The mainloop() function keeps the application running and waits for user interactions.

3. Add the Display

We need a place where the user can see the numbers and mathematical expressions.

A simple Entry widget can be used for this:

display = tk.Entry(root, font=("Arial", 24))
display.pack(fill="both", padx=10, pady=10)
Enter fullscreen mode Exit fullscreen mode

Now we have a basic calculator display.

4. Create Button Actions

The calculator needs to respond when the user presses a button.

For example, we can create a function that adds a value to the display:

def add_to_display(value):
    display.insert(tk.END, value)
Enter fullscreen mode Exit fullscreen mode

We can then connect this function to our calculator buttons.

5. Create the Calculator Buttons

Instead of creating every button manually, we can store the button labels in a list:

buttons = [
    "7", "8", "9", "/",
    "4", "5", "6", "*",
    "1", "2", "3", "-",
    "0", ".", "=", "+"
]
Enter fullscreen mode Exit fullscreen mode

We can iterate through this list and create the buttons dynamically.

This keeps the code shorter and makes the interface easier to modify later.

6. Calculate the Result

When the user presses the equals button, we need to evaluate the mathematical expression.

For a simple educational project, Python's eval() function can be used:

def calculate():
    expression = display.get()

    try:
        result = eval(expression)
        display.delete(0, tk.END)
        display.insert(0, result)
    except Exception:
        display.delete(0, tk.END)
        display.insert(0, "Error")
Enter fullscreen mode Exit fullscreen mode

However, eval() should not be used blindly with arbitrary user input in real-world applications because it can execute Python code.

For a learning project where the input is controlled, it can help demonstrate the basic idea, but production applications should use a safer expression parser.

7. Add a Clear Button

We also need a way to clear the current expression:

def clear_display():
    display.delete(0, tk.END)
Enter fullscreen mode Exit fullscreen mode

The clear button can then call this function.

Putting Everything Together

After combining the window, display, buttons, and functions, we have the basic structure of a graphical calculator.

From here, the project can be improved in many ways:

  • Better button styling
  • Keyboard support
  • Error handling
  • Parentheses
  • Percentage calculations
  • Calculation history
  • Dark mode
  • More advanced mathematical operations

These improvements are also good exercises if you're using the project to practice Python.

Full Project

If you want to see the complete implementation and the finished project, I have also published a full version of this Python/Tkinter calculator on my website:

Build a Graphical Calculator with Python and Tkinter

The complete version includes the full source code and the final graphical interface.

Conclusion

Building a calculator with Python and Tkinter is a simple project, but it teaches several useful concepts at the same time.

You practice Python functions, GUI widgets, event handling, user input, loops, and basic application structure.

Once you're comfortable with this project, you can use the same concepts to build more advanced desktop applications with Python and Tkinter.

Top comments (0)