In the previous lesson, we learned about Database Connectivity and how Python applications interact with databases to store and manage data. Today, we will learn about GUI Programming, which allows developers to create graphical user interfaces for desktop applications.
Python provides a built-in library called Tkinter for building GUI applications.
GUI Programming helps developers create applications with:
- Windows
- Buttons
- Labels
- Text boxes
- Menus
- Interactive forms
What is GUI?
GUI stands for Graphical User Interface.
A GUI allows users to interact with applications visually instead of using command-line text only.
Examples of GUI applications include:
- Calculators
- Text Editors
- Login Systems
- Music Players
- Banking Applications
What is Tkinter?
Tkinter is Python’s standard GUI library.
Tkinter helps programmers:
- Create windows
- Add buttons and labels
- Build forms
- Handle user interaction
- Design desktop applications
Tkinter comes pre-installed with Python.
Importing Tkinter
Example
import tkinter
A common shortcut is:
import tkinter as tk
Creating a Simple Window
Example
import tkinter as tk
window = tk.Tk()
window.mainloop()
This creates an empty GUI window.
Understanding mainloop()
The "mainloop()" method keeps the window running and responsive.
Without it, the window closes immediately.
Setting Window Title
Example
import tkinter as tk
window = tk.Tk()
window.title("Python GUI")
window.mainloop()
Setting Window Size
Example
import tkinter as tk
window = tk.Tk()
window.geometry("400x300")
window.mainloop()
This creates a window with:
- Width = 400
- Height = 300
Adding a Label
Labels display text in a GUI application.
Example
import tkinter as tk
window = tk.Tk()
label = tk.Label(window, text="Welcome to Python GUI")
label.pack()
window.mainloop()
Adding a Button
Buttons allow users to perform actions.
Example
import tkinter as tk
window = tk.Tk()
button = tk.Button(window, text="Click Me")
button.pack()
window.mainloop()
Handling Button Click Events
Functions can be connected to buttons.
Example
import tkinter as tk
def greet():
print("Welcome to Python")
window = tk.Tk()
button = tk.Button(window, text="Click", command=greet)
button.pack()
window.mainloop()
Adding an Entry Widget
The "Entry" widget accepts user input.
Example
import tkinter as tk
window = tk.Tk()
entry = tk.Entry(window)
entry.pack()
window.mainloop()
Getting User Input from Entry
Example
import tkinter as tk
def show_text():
print(entry.get())
window = tk.Tk()
entry = tk.Entry(window)
entry.pack()
button = tk.Button(window, text="Submit", command=show_text)
button.pack()
window.mainloop()
Adding a Text Widget
The "Text" widget handles multiple lines of text.
Example
import tkinter as tk
window = tk.Tk()
text = tk.Text(window)
text.pack()
window.mainloop()
Using Frames
Frames help organize widgets inside a window.
Example
import tkinter as tk
window = tk.Tk()
frame = tk.Frame(window)
frame.pack()
window.mainloop()
Message Boxes
Tkinter provides message boxes for alerts and notifications.
Example
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
messagebox.showinfo("Message", "Welcome to Python")
window.mainloop()
Simple GUI Calculator
Example
import tkinter as tk
def calculate():
result = int(entry1.get()) + int(entry2.get())
label.config(text="Result = " + str(result))
window = tk.Tk()
entry1 = tk.Entry(window)
entry1.pack()
entry2 = tk.Entry(window)
entry2.pack()
button = tk.Button(window, text="Add", command=calculate)
button.pack()
label = tk.Label(window, text="")
label.pack()
window.mainloop()
Advantages of GUI Programming
GUI Programming helps applications:
- Become user-friendly
- Improve user interaction
- Provide visual interfaces
- Simplify navigation
- Create professional applications
Most desktop applications use GUI systems.
Real-World Applications of GUI Programming
GUI applications are used in:
- Banking Systems
- Text Editors
- POS Systems
- School Management Systems
- Media Players
- Desktop Tools
Conclusion
GUI Programming is an important part of Python development because it allows programmers to create interactive desktop applications.
Tkinter provides a simple and powerful way to build GUI applications using Python.
Practice creating windows, buttons, labels, and forms to strengthen your Python GUI programming skills.
Top comments (0)