06 – Real-World Python Projects – GUI App using Tkinter

๐ŸŽฏ Project Objective

To build a Graphical User Interface (GUI) application in Python using Tkinter, enabling users to interact with programs visually instead of via the console.

Skills Demonstrated:

  • GUI development using Tkinter
  • Widgets: labels, buttons, entries, listboxes
  • Event handling and user interaction
  • Integrating Python logic into GUI apps

Project: GUI Calculator App

Project Description

The GUI Calculator app allows users to perform basic arithmetic operations (addition, subtraction, multiplication, division) using buttons and an interactive interface.

Features:

  • Clickable buttons for numbers and operations
  • Display input and result in a text field
  • Clear button to reset input
  • Handles errors like division by zero

Python Example Code

import tkinter as tk
from tkinter import messagebox

def click_button(value):
    entry_field.insert(tk.END, value)

def clear():
    entry_field.delete(0, tk.END)

def calculate():
    try:
        result = eval(entry_field.get())
        entry_field.delete(0, tk.END)
        entry_field.insert(tk.END, str(result))
    except Exception as e:
        messagebox.showerror("Error", f"Invalid Input: {e}")

# Create main window
root = tk.Tk()
root.title("Python GUI Calculator")

# Entry field
entry_field = tk.Entry(root, width=16, font=("Arial", 24), borderwidth=2, relief="solid")
entry_field.grid(row=0, column=0, columnspan=4)

# Buttons layout
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),
    ('0',4,0), ('.',4,1), ('=',4,2), ('+',4,3),
]

for (text, row, col) in buttons:
    if text == "=":
        tk.Button(root, text=text, width=5, height=2, command=calculate).grid(row=row, column=col)
    else:
        tk.Button(root, text=text, width=5, height=2, command=lambda t=text: click_button(t)).grid(row=row, column=col)

# Clear button
tk.Button(root, text='C', width=5, height=2, command=clear).grid(row=5, column=0, columnspan=4, sticky="we")

root.mainloop()

โœ… Output: A functional GUI calculator window with buttons and interactive input/output.


โœ… Key Features

  • User-friendly graphical interface
  • Interactive buttons and input fields
  • Real-time calculation or interaction
  • Error handling and user feedback

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *