Fundamentals 5 min read

Building a Simple Tkinter Calculator in Python – A Beginner’s Guide

This article introduces a beginner-friendly Python GUI project that builds a simple desktop calculator using Tkinter, covering the background, core functionalities, step-by-step code explanations, and highlights such as clean UI, clear logic, and extensibility for further enhancements.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Building a Simple Tkinter Calculator in Python – A Beginner’s Guide

Recently I started learning Python GUI programming and created a simple desktop calculator with Tkinter. Although the functionality is basic, the project is ideal for beginners to practice.

Project Background

Tkinter is the standard GUI toolkit bundled with Python, requiring no third‑party libraries and offering good cross‑platform compatibility. This calculator helps you grasp fundamental Tkinter concepts such as window creation, layout management, and button event handling.

Project Features

Basic arithmetic operations (addition, subtraction, multiplication, division)

Real‑time display of input and results

Clear (C) button to reset the expression

Simple error‑handling mechanism

Core Code Explanation

Creating the Main Window

root = tk.Tk()
root.title("简易计算器")
root.geometry("300x400")

Defining the Button Layout

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

Handling User Input and Evaluation

def on_click(self, button):
    if button == "C":
        self.expression = ""
    elif button == "=":
        try:
            self.expression = str(eval(self.expression))
        except:
            self.expression = "错误"
    else:
        self.expression += button
    self.input_text.set(self.expression)

Project Highlights

Clean UI : Uses the pack geometry manager for automatic layout adaptation across window sizes.

Clear Logic : Organized with object‑oriented programming for easy readability.

Beginner Friendly : No complex syntax, making it suitable for newcomers.

Source Code

import tkinter as tk

class Calculator:
    def __init__(self, root):
        self.root = root
        self.root.title("简易计算器")
        self.root.geometry("300x400")
        self.expression = ""
        self.input_text = tk.StringVar()
        self.create_widgets()

    def create_widgets(self):
        input_frame = tk.Frame(self.root, height=50, bg="lightgray")
        input_frame.pack(expand=True, fill="both")
        input_field = tk.Entry(input_frame, textvariable=self.input_text, font=("Arial", 20), bd=10, justify="right")
        input_field.pack(expand=True, fill="both")
        button_frame = tk.Frame(self.root)
        button_frame.pack(expand=True, fill="both")
        buttons = [
            ["7", "8", "9", "/"],
            ["4", "5", "6", "*"],
            ["1", "2", "3", "-"],
            ["C", "0", "=", "+"]
        ]
        for row in buttons:
            row_frame = tk.Frame(button_frame)
            row_frame.pack(expand=True, fill="both")
            for btn_text in row:
                btn = tk.Button(row_frame, text=btn_text, font=("Arial", 18), bd=5,
                                 command=lambda x=btn_text: self.on_click(x))
                btn.pack(side="left", expand=True, fill="both")

    def on_click(self, button):
        if button == "C":
            self.expression = ""
        elif button == "=":
            try:
                self.expression = str(eval(self.expression))
            except:
                self.expression = "错误"
        else:
            self.expression += button
        self.input_text.set(self.expression)

if __name__ == "__main__":
    root = tk.Tk()
    calc = Calculator(root)
    root.mainloop()

Conclusion

This lightweight Python GUI project is perfect for beginners to practice Tkinter. You can extend it by adding features such as decimal support, parentheses, or a calculation history.

GUIPythonDesktopTkinterBeginnerCalculator
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.