Skip to content

Python GUI Programming

GUI (Graphical User Interface) applications allow users to interact with programs through graphical elements like windows, buttons, text boxes, etc., rather than through the command line. Python provides multiple libraries for creating desktop GUI applications.

This chapter will introduce several of the most popular Python GUI libraries.

Tkinter: Python's Standard GUI Library

Tkinter is Python's standard GUI library, built into Python's standard installation, so no additional installation is required. This makes it an excellent starting point for learning GUI programming and creating simple applications.

Advantages of Tkinter:

  • Lightweight and cross-platform (Windows, macOS, Linux).
  • Built into Python, no installation required.
  • Simple and easy to learn, suitable for beginners.

Disadvantages of Tkinter:

  • The default interface looks somewhat outdated.
  • For very complex interfaces, layout management can become cumbersome.

Example: A Simple "Hello, World!" Program

python
import tkinter as tk
from tkinter import ttk

# 1. Create main window
window = tk.Tk()
window.title("My First GUI App")
window.geometry("300x200") # Set window size

# 2. Create a Label widget
label = ttk.Label(window, text="Hello, Tkinter!")
label.pack(pady=20) # .pack() is a simple layout manager

# 3. Create a Button widget
def on_button_click():
    label.config(text="Button was clicked!")

button = ttk.Button(window, text="Click Me", command=on_button_click)
button.pack()

# 4. Start event loop
# The event loop listens for user actions (like clicking buttons, moving windows) and responds
window.mainloop()

PyQt / PySide: Powerful Qt Framework

Qt is a mature, powerful cross-platform C++ GUI framework, widely used for developing professional desktop applications (e.g., KDE desktop environment, VLC media player). Python provides bindings to Qt through two main libraries:

  • PyQt: Developed by Riverbank Computing, available under commercial or GPL license.
  • PySide: Officially supported by Qt Company, available under more permissive LGPL license.

Both APIs are very similar. They both provide all the tools needed to create modern, feature-rich GUI applications.

Advantages:

  • Capable of creating very professional and beautiful interfaces.
  • Has a vast widget library and powerful features (such as networking, database, multimedia support).
  • Excellent documentation and community support.

Disadvantages:

  • Steeper learning curve than Tkinter.
  • Requires separate installation.

wxPython: Another Mature Option

wxPython is a wrapper around the wxWidgets C++ library. One of its main features is that it uses the native widgets of the target operating system as much as possible, making applications look and feel "native" on different platforms.

Advantages:

  • Native appearance and feel.
  • Has an active and friendly community.

Disadvantages:

  • Slightly lower popularity in certain areas compared to PyQt/PySide.

Kivy: For Multi-touch Applications

Kivy is an open-source Python library for rapidly developing applications. Its uniqueness lies in that it was originally designed to support innovative user interfaces, especially multi-touch applications. Programs written in Kivy can be easily packaged and run on iOS, Android, Linux, macOS, and Windows.

Advantages:

  • Truly cross-platform, especially excels on mobile.
  • Designed for modern, non-traditional interface design.

How to Choose?

  • For Beginners or Simple Tools: Tkinter is the best choice to start with.
  • For Complex Professional Desktop Applications: PyQt or PySide are industry standards.
  • If You Pursue Native Look: wxPython is a good choice.
  • If You Want to Develop Touch Screen or Mobile Applications: Kivy could be a good choice.

Content is for learning and research only.