Create a Simple Calculator App using Python Tkinter

Simple Calculator to develop application

Introduction to Simple Calculator Application

This tutorial walks you through the process of creating a simple calculator application using Python's Tkinter library. It is a great beginner project for those learning GUI programming in Python. The calculator allows basic arithmetic operations such as addition, subtraction, multiplication, and division. We've also kept the code minimal to ensure it is easy to follow and learn from.

Python Code for the Simple Calculator

Below is the complete Python code for the simple calculator application:
import tkinter as tk

def on_click(event):
    text = event.widget.cget("text")
    if text == "=":
        try:
            result = eval(str(screen.get()))
            screen.set(result)
        except Exception as e:
            screen.set("Error")
    elif text == "C":
        screen.set("")
    else:
        current = screen.get()
        screen.set(current + text)

# Creating main window
root = tk.Tk()
root.title("Simple Calculator")
root.geometry("300x400")

# StringVar for the screen
tk.Label(root, text="Calculator", font="lucida 20 bold").pack()
screen = tk.StringVar()
screen_entry = tk.Entry(root, textvar=screen, font="lucida 20 bold", justify='right')
screen_entry.pack(fill=tk.BOTH, ipadx=8, pady=10, padx=10)

# Adding buttons
button_frame = tk.Frame(root)
button_frame.pack()

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

for (text, row, col) in buttons:
    button = tk.Button(button_frame, text=text, font="lucida 15 bold", width=4, height=2)
    button.grid(row=row, column=col, padx=5, pady=5)
    button.bind("<Button-1>", on_click)

# Running the main loop
root.mainloop()

Creating an Executable File using PyInstaller

After creating the calculator application, you can convert it into a standalone executable file (.exe) using PyInstaller. Here’s how:
1. Install PyInstaller:
pip install pyinstaller
2. Run PyInstaller on your Python file to create an executable:
pyinstaller --onefile your_script.py
This command will bundle your calculator script into a single executable file, allowing you to share it without requiring users to have Python installed.

Conclusion

This simple calculator project is a great starting point for those new to GUI programming in Python. It demonstrates how to create buttons, take user input, and handle events. Once complete, you can convert your Python script into a standalone executable using PyInstaller, making it easier to distribute your application.

More on Pyinstaller to create Standalone Tkinter Apps by Converting to .exe file

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer