Welcome back to PyMakers, your creative coding playground. In this project, we’re diving into the world of graphical user interfaces (GUI) and mathematics by building a “Calculator with GUI” using Python. This interactive tool is designed to help you perform calculations with flair and ease.

Why a Calculator with GUI?

Calculators are essential for everyday math tasks, and adding a graphical user interface (GUI) makes them even more user-friendly. By creating a Calculator with GUI in Python, you’ll not only learn about GUI development but also have a stylish and functional tool for performing calculations. It’s a project that combines math and aesthetics.

The Python Code

Let’s dive right into the Python code for our Calculator with GUI:

import tkinter as tk

# Function to handle button clicks
def button_click(event):
    current = entry.get()
    text = event.widget.cget("text")

    if text == "=":
        try:
            result = str(eval(current))
            entry.delete(0, tk.END)
            entry.insert(tk.END, result)
        except Exception as e:
            entry.delete(0, tk.END)
            entry.insert(tk.END, "Error")
    elif text == "C":
        entry.delete(0, tk.END)
    else:
        entry.insert(tk.END, text)

app = tk.Tk()
app.title("Calculator with GUI")

entry = tk.Entry(app, font=("Helvetica", 16))
entry.pack(fill=tk.BOTH, expand=True)

button_frame = tk.Frame(app)
button_frame.pack()

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

row, col = 1, 0
for button in buttons:
    btn = tk.Button(button_frame, text=button, font=("Helvetica", 12))
    btn.grid(row=row, column=col, padx=10, pady=10)
    btn.bind("<Button-1>", button_click)
    col += 1
    if col > 3:
        col = 0
        row += 1

app.mainloop()

How it Works

  1. We import the tkinter library to create the GUI for the calculator.
  2. We define a function, button_click(), that handles button clicks and performs the calculations.
  3. Inside the main application, we create an entry field to display the current input and result.
  4. We create buttons for digits, arithmetic operations, and special buttons like "C" (clear) and "=" (calculate).
  5. Buttons are arranged in a grid within the button_frame.
  6. Button clicks are bound to the button_click() function, which updates the display based on user input.

Conclusion

Congratulations! You’ve just created a Python Calculator with a graphical user interface (GUI). This project introduces you to GUI development and provides a stylish and interactive solution for performing calculations.

Whether you’re crunching numbers for school, work, or personal projects, this Calculator with GUI adds a touch of style and ease to your mathematical tasks. Stay tuned for more Python projects that bring creativity and functionality to your coding journey. Happy coding! 🐍✨