Graphical User Interface (GUI) in Python
A Graphical User Interface (GUI) is a user-friendly interface that allows interaction with
software applications using graphical elements such as windows, buttons, menus, and icons
instead of command-line interfaces.
Python offers several GUI frameworks that help in building interactive applications. Some of the
most popular ones are:
Different GUI Tools in Python
1. Tkinter
● Built-in GUI toolkit in Python (part of the standard library).
● Simple and easy to use for basic GUI applications.
● Uses Tk as the underlying framework.
● Provides various widgets like buttons, labels, text fields, frames, and canvas.
Example:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Hello, Tkinter!")
label.pack()
root.mainloop()
●
2. PyQt (Qt for Python)
● Developed by Riverbank Computing.
● A powerful, feature-rich GUI framework based on Qt.
● Uses an event-driven programming model.
● Supports both PyQt5 and PyQt6.
● More complex but provides a lot of customization.
Example:
from PyQt5.QtWidgets import QApplication, QLabel
import sys
app = QApplication(sys.argv)
label = QLabel('Hello, PyQt!')
label.show()
sys.exit(app.exec_())
●
3. Kivy
● Open-source multi-platform GUI framework.
● Used for mobile and desktop applications.
● Built-in support for multitouch, gestures, and animations.
● Requires OpenGL for rendering.
Example:
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
return Label(text='Hello, Kivy!')
MyApp().run()
●
4. PySide (Qt for Python)
● Developed by Qt Company (alternative to PyQt).
● API is similar to PyQt.
● Uses LGPL license, making it suitable for commercial applications.
5. wxPython
● Cross-platform GUI toolkit.
● Uses native components, so it looks like a standard desktop application.
● Supports widgets like frames, buttons, text controls.
6. PyGTK
● Based on GTK+ toolkit.
● Supports Linux, Windows, and MacOS.
● Provides an object-oriented API for GUI development.
Working with Containers
Containers are widgets that hold other widgets and help in organizing GUI elements.
Types of Containers in Python GUI
1. Frame – Used to group related widgets.
2. Canvas – Used for drawing shapes and custom graphics.
3. PanedWindow – Allows resizing of child widgets.
4. Notebook – Provides tabbed navigation.
Example:
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, width=300, height=200, bg="lightblue")
frame.pack()
root.mainloop()
Canvas
● A drawing area that allows users to create custom graphics, shapes, images, and
animations.
● Supports event handling for mouse clicks, key presses, etc.
Example:
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=200, bg="white")
canvas.pack()
canvas.create_rectangle(50, 50, 200, 150, fill="blue")
root.mainloop()
Frame
● A container widget used to group other widgets together.
● Helps in layout management.
Example:
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, width=200, height=100, bg="gray")
frame.pack()
tk.Label(frame, text="Inside Frame").pack()
root.mainloop()
Widgets in Python GUI
Widgets are building blocks of any GUI application.
Commonly Used Widgets
Widget Description
Button Clickable button for triggering actions.
Label Displays text or images.
Entry Single-line text input field.
Text Multi-line text input area.
Scrollbar Adds scrolling capability to other widgets.
Checkbutton Allows multiple selections from options.
Radiobutton Allows a single selection from options.
Spinbox A small input box for numeric values.
Message Displays multi-line text like a label but supports word wrapping.
1. Button
Used to trigger actions when clicked.
import tkinter as tk
def on_click():
print("Button Clicked!")
root = tk.Tk()
btn = tk.Button(root, text="Click Me", command=on_click)
btn.pack()
root.mainloop()
2. Label
Displays text or images.
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Hello, World!", font=("Arial", 16))
label.pack()
root.mainloop()
3. Entry (Single-line Text Field)
Used for text input.
import tkinter as tk
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
root.mainloop()
4. Text (Multi-line Text Box)
Allows multi-line text input.
import tkinter as tk
root = tk.Tk()
text_box = tk.Text(root, height=5, width=30)
text_box.pack()
root.mainloop()
5. Scrollbar
Used with Text, Canvas, or Listbox for scrolling.
import tkinter as tk
root = tk.Tk()
text_box = tk.Text(root, height=5, width=30)
text_box.pack(side="left")
scroll = tk.Scrollbar(root, command=text_box.yview)
scroll.pack(side="right", fill="y")
text_box.config(yscrollcommand=scroll.set)
root.mainloop()
6. Checkbutton
Allows multiple selections.
import tkinter as tk
root = tk.Tk()
var1 = tk.IntVar()
chk = tk.Checkbutton(root, text="Option 1", variable=var1)
chk.pack()
root.mainloop()
7. Radiobutton
Allows a single selection from multiple options.
import tkinter as tk
root = tk.Tk()
var = tk.IntVar()
rb1 = tk.Radiobutton(root, text="Option A", variable=var, value=1)
rb2 = tk.Radiobutton(root, text="Option B", variable=var, value=2)
rb1.pack()
rb2.pack()
root.mainloop()
8. Spinbox
Used for selecting numerical values.
import tkinter as tk
root = tk.Tk()
spin = tk.Spinbox(root, from_=0, to=10)
spin.pack()
root.mainloop()
9. Message
Similar to Label but supports multi-line text wrapping.
import tkinter as tk
root = tk.Tk()
msg = tk.Message(root, text="This is a message widget that supports multiple lines of text.")
msg.pack()
root.mainloop()
Conclusion
Python provides several powerful GUI frameworks such as Tkinter, PyQt, and Kivy to create
interactive applications. Understanding containers, widgets, and event-driven programming
helps in building professional and user-friendly applications.