Learn GUI application development from the ground up by building simple projects that teach the fundamentals of using PyQt6. This 2nd edition includes updated code, programs, and new chapters to get you started using the newest version. Taking a practical approach, each chapter will gradually teach more advanced and diverse concepts to aid you in designing and customizing interesting and professional applications.
You'll start by learning important concepts related to GUI development, and then jump right into building different and exciting projects in every chapter. Along the way, you’ll discover new widgets, layouts, and other concepts that will help you to build better UIs. You'll also construct more elaborate GUIs, covering topics that include storing data using the clipboard, graphics and animation, support for SQL databases, multithreading applications, and building modern-looking interfaces.
Using this knowledge, you’ll be able to build a photo editor, games, a text editor, a working web browser, and an assortment of other GUIs. In the end, this book will guide you through the process of creating UIs to help you bring your own ideas to life. Find out what you need to begin making your own applications with PyQt!
What You'll Learn
- Develop cross-platform UIs with PyQt and Python
- Use PyQt’s many widgets and apply them by building real applications
- Build larger applications through a step-by-step approach and break the code into smaller chunks for deeper understanding
- Work with more complex applications in PyQt, covering SQL databases, multithreading, web browsers, and more
- Create modern-looking UIs with Qt Quick and QtQml using the latest version of PyQt
Who This Book Is For
Python developers who are looking to begin creating user interfaces and want to utilize the latest version of PyQt to gets started. Having prior knowledge of PyQt or other Python UI toolkits is not necessary to begin using this book.
Author(s): Joshua M Willman
Edition: 2
Publisher: Apress
Year: 2022
Language: English
Pages: 565
Table of Contents
About the Author
About the Technical Reviewers
Acknowledgments
Introduction
Chapter 1: Getting Started with PyQt
The PyQt Framework
Why Choose PyQt?
PyQt5 vs. PyQt6
Installing Python 3 and PyQt6
Checking Your Version of Python
Installing PyQt6
Introduction to User Interfaces
What Is a Graphical User Interface?
Concepts for Creating Good Interface Design
Creating Your First GUI: An Empty Window
Explanation for Creating an Empty Window
Modifying the Window
Summary
Chapter 2: Building a Simple GUI
The QLabel Widget
Explanation for Using QLabel
Project 2.1 – User Profile GUI
Designing the User Profile GUI
Explanation for the User Profile GUI
Summary
Chapter 3: Adding More Functionality with Widgets
Event Handlers and Signals and Slots
The QPushButton Widget
Explanation for Using QPushButton
The QLineEdit Widget
Explanation for Using QLineEdit
The QCheckBox Widget
Explanation for Using QCheckBox
The QMessageBox Dialog
Windows vs. Dialogs
Explanation for Using QMessageBox
Project 3.1 – Login GUI and Registration Dialog
Designing the Login GUI and Registration Dialog
Explanation for Creating the Login GUI
How to Open a New Window or Dialog
Using Event Handlers to Close a Window
Explanation for Creating the Main Window
Explanation for Creating the Registration Dialog
Summary
Chapter 4: Learning About Layout Management
Using Layout Managers in PyQt
Absolute Positioning
Horizontal and Vertical Layouts with Box Layouts
Explanation for QHBoxLayout
Explanation for QVBoxLayout
The QButtonGroup Class
Creating Nested Layouts
Explanation for Nested Layouts
The QSpinBox and QComboBox Widgets
Combining Layouts and Arranging Widgets
Arranging Widgets in Grids with QGridLayout
Explanation for QGridLayout
The QTextEdit Widget
Adding Widgets and Spanning Rows and Columns in QGridLayout
Finding Child Widgets in a Layout
Building Forms with QFormLayout
Explanation for QFormLayout
Adding Widgets and Layouts to QFormLayout
Managing Pages with QStackedLayout
Explanation for QStackedLayout
Additional Tips for Managing Space
Explanation for Managing Space
Setting Content Margins
Summary
Chapter 5: Menus, Toolbars, and More
Common Practices for Creating Menus
Creating a Simple Menu Bar
Explanation for Creating a Menu Bar
QMainWindow vs. QWidget
Creating the Menu Bar and Adding Actions
Using Icons and the QIcon Class
Explanation for Using Icons
Setting the Central Widget
Built-in Dialog Classes in PyQt
The QFileDialog Class
The QInputDialog Class
The QFontDialog Class
The QColorDialog Class
The About QMessageBox
Project 5.1 – Rich Text Notepad GUI
Designing the Rich Text Notepad GUI
Explanation for the Rich Text Notepad GUI
Expanding the Features in a Main Window
Explanation for Expanding the Features
The QStatusBar Class
Creating Submenus with Checkable Menu Items
The QToolBar Class
The QDockWidget Class
Project 5.2 – Simple Photo Editor GUI
Designing the Photo Editor GUI
Explanation for the Photo Editor GUI
Handling Images in the Photo Editor GUI
The QPrinter Class
Summary
Chapter 6: Styling Your GUIs
What Are Styles in PyQt?
Changing the Default Style
Modifying Widget Appearances
Using HTML to Change the Look of Text
Using Qt Style Sheets to Change the Look of Widgets
Explanation for Using “Inline” Qt Style Sheets
Customizing Styles to React to Interactions
Explanation for Using “Embedded” Qt Style Sheets
Applying Changes to Specific Widgets
Organizing Widgets with Containers and Tabs
The QRadioButton Widget
The QGroupBox Class
The QTabWidget Class
Explanation for Using Containers and Tabs
Project 6.1 – Food Ordering GUI
Design the Food Ordering GUI
Explanation for the Food Ordering GUI
Creating the Style Sheet
Building the Main Window
CSS Properties Reference
Summary
Chapter 7: Handling Events in PyQt
Event Handling in PyQt
Using Signals and Slots
Using Event Handlers to Handle Events
Difference Between Signals and Slots and Event Handlers
Handling Key Events
Explanation for Handling Key Events
Handling Mouse Events
Explanation for Handling Mouse Events
Creating Custom Signals
Explanation for Creating Custom Signals
Summary
Chapter 8: Creating GUIs with Qt Designer
Getting Started with Qt Designer
Installing Qt Designer
Exploring Qt Designer’s User Interface
Qt Designer’s Editing Modes
Creating an Application in Qt Designer
Project 8.1 – Keypad GUI
Explanation for the Keypad GUI
Selecting a Form
Arranging Objects on the Form
The QFrame Class
Applying Layouts in Qt Designer
Editing the Properties of Objects
Previewing Your GUI
Connecting Signals and Slots in Qt Designer
Creating Python Code from Qt Designer
Generating Code Using pyuic6
Creating a New Script to Build a GUI
Extra Tips for Using Qt Designer
Setting Up Main Windows and Menus
Adding Menus and Submenus in Qt Designer
Adding Toolbars in Qt Designer
Adding Actions in Qt Designer
Displaying Images in Qt Designer
Adding Style Sheets in Qt Designer
Summary
Chapter 9: Working with the Clipboard
The QClipboard Class
Explanation for Using QClipboard
Project 9.1 – Sticky Notes GUI
Explanation for the Sticky Notes GUI
Summary
Chapter 10: Presenting Data in PyQt
Quickly Handling Data in PyQt
The QListWidget Class
Explanation for Using QListWidget
Drag and Drop in PyQt
Explanation for Drag and Drop
The QTableWidget Class
Explanation for Using QTableWidget
Creating Context Menus
Using Built-in QTableWidget Methods to Edit Data
The QTreeWidget Class
Explanation for Using QTreeWidget
Summary
Chapter 11: Graphics and Animation in PyQt
Introduction to the QPainter Class
Explanation for Using the QPainter Class
The paintEvent() Event Handler
The QColor, QPen, and QBrush Classes
Drawing Points and Lines
Drawing Text
Drawing Two-Dimensional Shapes
Drawing Gradients
Project 11.1 – Painter GUI
Explanation for the Painter GUI
Creating the Canvas Class
Handling Mouse Movement Events
Creating the Painter GUI’s MainWindow Class
Creating Tool Tips for Widgets
Animating Scenes with QPropertyAnimation
Explanation for Animating Scenes
Introduction to Animating Widgets
Explanation for Animating Widgets
Summary
Chapter 12: Creating Custom Widgets
Project 12.1 – RGB Slider Custom Widget
PyQt’s Image Handling Classes
The QSlider Widget
Explanation for the RGB Slider Widget
Updating the Sliders and Spin Boxes
Updating the Colors
Adding Methods to a Custom Widget
RGB Slider Demo
Explanation for the RGB Slider Demo
Summary
Chapter 13: Working with Qt Quick
Outlining QtQuick and QML
Elements in QtQuick
Introduction to the QML Language and Syntax
Explanation for QML Language and Syntax
Defining Properties of an Element
The Coordinate System
Building and Running QML Components
Creating and Loading QML Components
Explanation for Creating QML Components
Positioning Elements with Anchors
Adding Images in QtQuick
Explanation for Loading QML Components
Creating Reusable Components
Explanation for Creating Custom Components
Making an Element Interactive with Mouse Handling
Layout Handling in QML
Using Positioners to Position Elements
Explanation for Using Column and Grid Positioners
Building and Loading QML Windows
QtQuick Controls
Explanation for Creating QML Windows
Creating a Menu Bar
Signals and Signal Handlers
Using FileDialog to Open Files
Explanation for Loading QML Windows
Using Transformations to Animate Objects
Explanation for Simple Transformations
Explanation for Using Transformations to Animate Objects
Summary
Chapter 14: Introduction to Handling Databases
Thinking About Data
Introduction to Model/View Programming
The Components of the Model/View Architecture
PyQt’s Model/View Classes
Explanation for Introduction to Model/View
Setting Up the Model, View, and Selection Modes
Working with CSV Files
Working with SQL Databases in PyQt
What Is SQL?
Working with Database Management Systems
Getting Familiar with SQL Commands
Project 14.1 – Account Management GUI
Explanation for Working with the QtSql Module
Creating a Connection to a Database
Building a Dataset with QSqlQuery
Visualizing SQL Data Using SQLite
Explanation for Querying a Database with QSqlQuery
Working with the QSqlTableModel Class
Explanation for Working with QSqlTableModel
Working with the QSqlRelationalTableModel Class
Explanation for Working with QSqlRelationalTableModel
Adding Delegates to Edit Relational Data
Explanation for the Account Management GUI
Summary
Chapter 15: Managing Threads
Introduction to Threading
Threading in PyQt
Methods for Processing Long Events in PyQt
Project 15.1 – File Renaming GUI
The QProgressBar Widget
Explanation for File Renaming GUI
Summary
Chapter 16: Extra Projects
Project 16.1 – Directory Viewer GUI
Explanation for the Directory Viewer GUI
Project 16.2 – Camera GUI
Explanation for the Camera GUI
Project 16.3 – Simple Clock GUI
Explanation for the Clock GUI
Project 16.4 – Calendar GUI
Explanation for the Calendar GUI
Project 16.5 – Hangman GUI
Explanation for the Hangman GUI
Creating the Drawing Class
Creating the Main Window Class
Project 16.6 – Web Browser GUI
Explanation for Web Browser GUI
Creating Tabs for the Web Browser
Creating the Web View
Adding a QProgressBar to the Status Bar
Project 16.7 – Tri-state QComboBox
Explanation for the Tri-state QComboBox
Summary
Appendix: Reference Guide for PyQt6
Selected PyQt6 Modules
Selected PyQt Classes
Classes for Building a GUI Window
QApplication
QWidget
Event Handling
QMainWindow
QDialog
QPainter
Layout Managers
Button Widgets
Input Widgets
Combo Boxes
QLineEdit
Text Editing Widgets
Spin Box Widgets
Slider Widgets
Display Widgets
QLabel
QProgressBar
QGraphicsView
QLCDNumber
Item Views
Container Widgets
Containers with Frames
QScrollArea
QMdiArea
QtQuick and QML
Qt Style Sheets
Qt Namespace
Summary
Index