Joshua M Willman

Beginning PyQt

A Hands-on Approach to GUI Programming with PyQt6

2nd ed.
Joshua M Willman
Sunnyvale, CA, USA
ISBN 978-1-4842-7998-4e-ISBN 978-1-4842-7999-1
© Joshua M Willman 2022
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

This Apress imprint is published by the registered company APress Media, LLC part of Springer Nature.

The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.

To my daughter, Kalani.

Introduction

With new PyQt versions come new tools to play with. The latest edition aims to explore some of those ideas while still being aimed toward beginners. You will explore how to use the Python programming language, along with the PyQt6 toolkit, to create graphical user interfaces (GUIs).

Just getting started is more important than anything else. Coding a GUI can be considered a combination of programming and graphic design skills. An awareness of a user’s needs is also crucial for better usability and graphical appearance. Programming a GUI is often a matter of selecting the right component, referred to as widgets in PyQt, to complete a task and then applying the necessary programming skills to make them operational.

One goal is to balance the theory behind good design practices with more hands-on, learn-as-you-go style coding examples. New concepts and PyQt classes are introduced in each chapter, and later chapters sometimes build upon previous ones.

Who Should Read This Book

This book is targeted to Python developers who are looking to begin creating graphical user interfaces and want to utilize the latest version of PyQt to get started. Having prior knowledge of PyQt or other Python GUI toolkits is not necessary to begin using this book. It is, however, recommended that you understand the fundamentals of Python and Python syntax and are comfortable using Object-Oriented Programming (OOP).

How This Book Is Organized

The latest edition of Beginning PyQt begins by introducing you to the basic ideas behind GUI development. Chapter 1 will get you on your way to installing and understanding how to use the latest version of PyQt.

Chapters 2 and 3 teach how to add widgets to your applications, thereby adding more and more functionality to your projects. Both chapters introduce different widgets, such as QLabel, QCheckBox, and QLineEdit, and give examples and ideas for using them. Chapter 3 will also introduce you to PyQt’s signals and slots mechanism for handling events.

Chapter 4 focuses on layout managers for arranging widgets.

After learning about different widgets and layouts, Chapter 5 guides you through examples that help you to create classical GUIs with menus and toolbars.

Chapter 6 presents style sheets for altering the look of your applications.

Chapter 7 discusses how to handle events that occur in a GUI, such as a user clicking on the mouse. You’ll also discover how to create your own signals and how to reimplement event handlers.

Since Qt also includes its own graphical user interface to help you create GUIs, we will take a look at how to use Qt Designer in Chapter 8.

From there, we’ll start learning more advanced concepts.

Chapter 9 introduces you to using the clipboard to copy and paste information between applications.

Chapter 10 shows how to handle data using PyQt’s item-based convenience classes. You’ll also find out how to add basic drag-and-drop functionality to widgets.

Chapter 11 introduces you to painting, graphics, and animation.

Customization is important in PyQt. Chapter 12 shows you how to build and use your own custom widgets.

Chapter 13 talks about Qt Quick for building fluid and dynamic applications.

Chapter 14 shows how to use build user interfaces that handle data with SQL databases and PyQt’s Model/View architecture.

Chapter 15 discusses multithreaded programming to avoid your applications from freezing.

Chapter 16 contains extra example projects to help you continue to gain extra practice and insight into creating applications with PyQt.

The Appendix includes additional information about different PyQt modules and classes. An additional coding example can also be found in the Appendix.

Understanding the Structure and Code of the Chapters

Code for the latest edition is generally broken apart into smaller pieces (unless the program is already a short example). This makes the code easier to digest and understand and also prevents users from code dumps (copying and pasting entire sections). You can always refer to the GitHub repository (link found in the “Links to the Source Code” section) to see the code examples in their entirety.

Also, be sure to pay close attention to the listing titles above each piece of code when following along. They give hints about each section as well as help you know what code example you are viewing. For example, Chapter 2 introduces labels. The code for explaining how to use labels is broken into two parts, Listing 1 and Listing 2. The first listing’s header appears like the following lines:
# labels.py
Listing 1

Setting up the main window to show how to use QLabel widgets

The second part of the application is listed as
# labels.py
Listing 2

The setUpMainWindow() method for displaying text and image labels

If a code snippet does not contain a listing number, then that code provides additional information but is not located in any of the files.

In addition, where necessary, important differences between PyQt5 and PyQt6 are pointed out in the text.

Another important note is that PyQt is designed to be cross-platform. No world is perfect though, and sometimes, additional explanations are given for developers to get their code running on Windows, macOS, or Linux (Ubuntu). Be sure to take a look at the comments or notes for clarity when running an application.

Finally, as you are reading along, keywords use bold font. File names, Python and PyQt module and class names, and bits of the code that are mentioned in the text are displayed using a different font, for example, QPushButton.

Links to the Source Code

The source code for Beginning PyQt: A Hands-on Approach to GUI Programming with PyQt6 can be found on GitHub at https://github.com/Apress/Beginning-PyQt--second-edition.

Reader Feedback

Your feedback, questions, and ideas are always welcome. If you have any questions about this book, PyQt version 5 or 6, or GUI development or would just like to leave a comment, you can always find me at [email protected].

Acknowledgments

I am beyond grateful to Divya Modi, Celestin Suresh John, and the wonderful team at Apress for granting me the opportunity to write the second edition of this book. My deepest appreciation goes to Divya for being there every step along the way.

A special thanks to Andrea Casadei whose inquiries greatly helped to improve this edition.

An immense thanks to the Python, PyQt, and Qt communities. I would also really like to thank Phil Thompson, the creator of PyQt.

I owe an enormous amount of gratitude to Richard Bronosky for giving me a chance when I needed it the most.

Ashish Naik, thank you for all of the support you provided my family and me from the very beginning.

Thanks to my mother, Valorie, and my sisters, Teesha and Jazzmin, for the support you have always given me.

I am deeply thankful to my wife, Evelyn Ye, whose continued patience with me makes these books possible.

To Kalani, you continue to be my motivation and inspiration.

Once again, thank you to the readers. I truly hope that ideas found within this book will fuel your creativity and benefit you in some way.

Table of Contents
About the Author
Joshua Willman
is a software engineer with more than 12 years of experience developing applications in mainly Python and C++. His career has allowed him to participate in many different fields, from robotics, machine learning, and computer vision to UI development, game development, and more. His first experience with PyQt was building an interface for simplifying the labeling process of datasets for machine learning. Ever since then, he’s been hooked!

In recent years, his passion for programming and all things visual has allowed him to participate in numerous projects. These include designing educational courses for mobile robotics and computer vision using Arduino and Raspberry Pi, building GUI applications, and working as a solo indie game developer. He currently works as a robotics engineer, a technical writer, and a content creator (learning web development in his spare time in order to build his own platform, redhuli.io). When he’s not working, he enjoys tinkering on robotics projects and spending time with his wonderful wife and daughter.

He is also the author of two books with Apress:
  • Beginning PyQt: A Hands-on Approach to GUI Programming (1st Edition)

  • Modern PyQt: Create GUI Applications for Project Management, Computer Vision, and Data Analysis

 
About the Technical Reviewers
Vikas Kumar
has more than seven years of combined experiences in avionics, aerospace, automotive, and healthcare industries in R&D and software development activities. He has been programming and developing desktop applications with C++, Qt, PyQt/PySide, Python, Java, and SQL since the beginning. He holds a Bachelor of Technology in Computer Science and Engineering from Biju Patnaik University of Technology, Odisha, India.

He has worked with various Indian defense clients for the development of avionics test suite for the testing of components of various military aircrafts. He has worked with Airbus as a client for the development of various software responsible for structural and computational analysis of various commercial aircrafts. He has worked with Mercedes-Benz for the development of software responsible for data analysis, simulation, modeling, and validation of high-voltage electric battery used in Mercedes-Benz electric vehicles.

His technical skills include C++, Qt, PyQt/PySide, Python, Java, MySQL, and desktop application development in Linux and Windows.

He currently works as Senior Software Engineer with GE Healthcare India for developing host software for MRI scanners.

 
Saumitra Jagdale
is the founder of Open Cloudware and Global AI Ambassador focusing on the current trends in technology. He is a recognized technical author for various established media houses like OpenSystems Media, CNX Software, AspenCore, Electronics-Lab, and IoT Tech Trends. Additionally, he is a Senior Engineer – Cloud Services and Software at L&T Infotech with expertise in the field of CRM and ERP applications. Being an open source Python developer, he also leads the TensorFlow Community India for promoting deep learning methodologies in the community.
 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset