Friday, August 22, 2025

๐ŸDID YOU KNOW YOU CAN BUILD GRAPHIC USER INTERFACES (GUI) USING PYTHON PROGRAMMING???

 

Python is one of the most popular programming languages today, but most beginners use it only for data analysis, scripting, or automation. Did you know you can also use Python to create Graphical User Interfaces (GUIs)?

That’s where Tkinter comes in.

In this guide, we’ll cover everything about Tkinter step by step, with clear examples, so you can start building your own apps with Python. By the end, you’ll be able to design forms, calculators, and even save data into Excel.


๐Ÿ”น What is Tkinter?

Tkinter is Python’s built-in GUI library.

  • It lets you create windows, buttons, textboxes, dropdowns, and other visual elements.

  • It comes pre-installed with Python (no need to install separately).

  • It is beginner-friendly and widely used for small apps, prototypes, and learning GUI programming.

✅ Check if Tkinter is available on your computer:

import tkinter print("Tkinter is installed!")

If you see no error, you’re ready to go.


๐Ÿ”น First Step: Creating a Tkinter Window

Every Tkinter program starts with:

  1. Creating a window

  2. Running it with .mainloop()

Example:

import tkinter as tk # Create main window root = tk.Tk() root.title("My First Tkinter Window") # Window title root.geometry("300x200") # Width x Height # Run the window root.mainloop()

✅ This will open a blank window with the title “My First Tkinter Window”.


๐Ÿ”น Adding Widgets (UI Elements)

Widgets are the elements inside the window:

  • Label → display text

  • Button → clickable action

  • Entry → input box

  • Text → multi-line input

  • Checkbutton/RadioButton → options

  • Listbox → list of items

Example:

import tkinter as tk root = tk.Tk() root.title("Widgets Example") root.geometry("300x200") # Label label = tk.Label(root, text="Hello Tkinter!", font=("Arial", 14)) label.pack(pady=10) # Button button = tk.Button(root, text="Click Me") button.pack(pady=10) # Textbox entry = tk.Entry(root) entry.pack(pady=10) root.mainloop()

✅ You now see a text label, button, and textbox stacked vertically.


๐Ÿ”น Handling Events (Making Buttons Work)

We can make buttons “do something” by linking them to a function.

import tkinter as tk def on_click(): user_text = entry.get() # Get text from textbox label.config(text=f"You typed: {user_text}") root = tk.Tk() root.title("Event Example") root.geometry("300x200") entry = tk.Entry(root) entry.pack(pady=5) button = tk.Button(root, text="Submit", command=on_click) button.pack(pady=5) label = tk.Label(root, text="Type something above") label.pack(pady=5) root.mainloop()

✅ Now when you type in the box and click Submit, the label updates with your input.


๐Ÿ”น Layout Management

Tkinter provides 3 layout managers to arrange widgets:

  1. .pack() → stacks elements vertically or horizontally.

  2. .grid(row, column) → places elements in a table format.

  3. .place(x, y) → places elements at fixed coordinates.

Example with grid (mini login form):

import tkinter as tk root = tk.Tk() root.title("Login Form") tk.Label(root, text="Username:").grid(row=0, column=0, padx=5, pady=5) tk.Entry(root).grid(row=0, column=1, padx=5, pady=5) tk.Label(root, text="Password:").grid(row=1, column=0, padx=5, pady=5) tk.Entry(root, show="*").grid(row=1, column=1, padx=5, pady=5) tk.Button(root, text="Login").grid(row=2, column=0, columnspan=2, pady=10) root.mainloop()

✅ This looks like a basic login form.


๐Ÿ”น Common Tkinter Widgets

Here are the most commonly used widgets with examples:

1. Labels

label = tk.Label(root, text="This is a label", font=("Arial", 12)) label.pack()

2. Buttons

def hello(): print("Hello, World!") button = tk.Button(root, text="Say Hello", command=hello) button.pack()

3. Entry (Textbox)

entry = tk.Entry(root, width=30) entry.pack()

4. Checkbutton

var = tk.IntVar() check = tk.Checkbutton(root, text="Accept Terms", variable=var) check.pack()

5. Radiobutton

var = tk.StringVar() tk.Radiobutton(root, text="Male", variable=var, value="Male").pack() tk.Radiobutton(root, text="Female", variable=var, value="Female").pack()

6. Listbox

listbox = tk.Listbox(root) listbox.insert(1, "Python") listbox.insert(2, "C++") listbox.insert(3, "Java") listbox.pack()

๐Ÿ”น Mini Project 1: A Simple Calculator

import tkinter as tk def calculate(): try: result = eval(entry.get()) label.config(text=f"Result: {result}") except: label.config(text="Error!") root = tk.Tk() root.title("Calculator") entry = tk.Entry(root, width=20) entry.pack(pady=5) button = tk.Button(root, text="Calculate", command=calculate) button.pack(pady=5) label = tk.Label(root, text="Result: ") label.pack(pady=5) root.mainloop()

✅ Type an expression like 5+3*2 → press Calculate → see result.


๐Ÿ”น Mini Project 2: Data Entry Form (Save to Excel)

We can extend Tkinter to save data into Excel using openpyxl.

Install:

pip install openpyxl

Code:

import tkinter as tk from openpyxl import Workbook, load_workbook import os def save_data(): name = entry_name.get() age = entry_age.get() city = entry_city.get() file = "data.xlsx" if os.path.exists(file): wb = load_workbook(file) ws = wb.active else: wb = Workbook() ws = wb.active ws.append(["Name", "Age", "City"]) # Header row ws.append([name, age, city]) wb.save(file) entry_name.delete(0, tk.END) entry_age.delete(0, tk.END) entry_city.delete(0, tk.END) label_status.config(text="Data Saved!") root = tk.Tk() root.title("Data Entry Form") root.geometry("300x250") tk.Label(root, text="Name:").pack() entry_name = tk.Entry(root) entry_name.pack() tk.Label(root, text="Age:").pack() entry_age = tk.Entry(root) entry_age.pack() tk.Label(root, text="City:").pack() entry_city = tk.Entry(root) entry_city.pack() tk.Button(root, text="Save", command=save_data).pack(pady=10) label_status = tk.Label(root, text="") label_status.pack() root.mainloop()

✅ Each time you fill the form and click Save, the data goes into data.xlsx.


๐Ÿ”น Next Steps with Tkinter

Once you master the basics, you can explore:

  • Menus & Toolbars

  • Frames & Tabs (Notebook widget)

  • Canvas (drawing shapes, images, charts)

  • Advanced styles with ttk (Themed Tkinter)

  • Packaging your Tkinter app as an .exe file with pyinstaller


๐ŸŽฏ Conclusion

Tkinter is a powerful yet simple way to start GUI programming in Python.

  • Start with windows, labels, buttons, entries.

  • Learn how to handle events.

  • Use grid/pack to arrange widgets.

  • Build mini projects like calculators, login forms, and data entry apps.

With Tkinter, you can turn your Python scripts into interactive apps that anyone can use without knowing Python.


Drop comments if you want to learn more...

No comments:

Post a Comment

๐ŸWhat is scikitlearn??