Creating a Simple GUI using
Tkinter
Unit IV: Graphical User Interfaces
By Aditya(27) , and gopal(29)
What is Tkinter?
•
Tkinter is the standard GUI (Graphical User
Interface) library for Python. It allows
developers to create windows, dialogs,
buttons, text fields, and more using simple
Python code. It is built on the Tcl/Tk GUI
toolkit and provides a quick way to create
interactive applications.
Understanding the Event Loop
•
Every GUI program waits for user interactions
such as mouse clicks or key presses. Tkinter
uses an event-driven model, where actions
(events) trigger specific functions (handlers).
The event loop (mainloop) continuously runs
and listens for such actions.
Creating the Main Window
•
To start building a GUI, we first create the
main window using Tk(). This is the foundation
where all widgets (buttons, labels, etc.) will be
placed. The window remains open using
mainloop().
Example:
import tkinter as tk
root = tk.Tk()
root.title("My First App")
Using Labels to Display Text
•
Labels are used to display static text or
images. They are created using the Label class.
You can customize text, font, size, and color.
Example:
label = tk.Label(root, text="Hello, World!",
font=("Arial", 16), fg="blue")
label.pack()
Adding Buttons to Trigger Actions
•
Buttons allow users to perform actions. A
function is linked to the button using the
'command' argument. When the button is
clicked, the function is executed.
Example:
def greet():
print("Hello!")
btn = tk.Button(root, text="Greet",
command=greet)
Taking User Input with Entry Fields
•
The Entry widget allows users to input text. It's
useful for login forms, data entry, etc. The
get() method retrieves the input value.
Example:
entry = tk.Entry(root)
entry.pack()
name = entry.get()
Dialog Boxes for Messages
•
Tkinter provides messagebox dialogs for
showing information, warnings, or errors.
These dialogs are helpful for alerts and
confirmations.
Example:
from tkinter import messagebox
messagebox.showinfo("Welcome", "You
clicked the button!")
Layout Management with pack()
•
The pack() method places widgets vertically or
horizontally. It's simple and automatic but
offers less control over precise placement.
Example:
label.pack()
entry.pack()
button.pack()
Using grid() for Table-Like Layout
•
grid() places widgets in rows and columns. It is
useful for structured layouts like forms.
Example:
tk.Label(root, text="Username").grid(row=0,
column=0)
tk.Entry(root).grid(row=0, column=1)
Absolute Positioning with place()
•
place() allows you to position widgets at exact
x and y coordinates. Use this method when
you need complete control over layout.
Example:
button.place(x=100, y=50)
Mini App: Combine Label, Entry,
and Button
•
You can combine multiple widgets to make a
small application. This example takes user
input and displays it.
Example:
def show_input():
name = entry.get()
messagebox.showinfo("Input", f"Hello,
{name}")
Styling and Customizing Widgets
•
Widgets can be styled using parameters like:
- font: ("Arial", 14, "bold")
- fg (foreground color), bg (background color)
- width, height
- padding (padx, pady)
Organizing with Frames
•
Frames act as containers to group related
widgets. Useful for complex UIs or nested
layouts.
Example:
frame = tk.Frame(root)
frame.pack()
tk.Label(frame, text="Inside frame").pack()
Putting It Together: Login Form
Example
•
A simple login form can be built using Labels,
Entries, and Buttons. You can validate input
and show messages.
Summary
•
- Tkinter is a powerful tool for creating GUI
apps in Python.
- Use widgets like Label, Button, Entry, and
Dialogs to interact with users.
- Manage layout using pack(), grid(), or place().
- Style and organize your interface with
attributes and frames.