Beginning PyQt A Hands-on Approach to GUI Programming with PyQt6 — Second Edition — Joshua M Willman
Beginning PyQt A Hands-on Approach to GUI Programming with PyQt6 Second Edition Joshua M Willman
Beginning PyQt: A Hands-on Approach to GUI Programming with PyQt6 ISBN-13 (pbk): 978-1-4842-7998-4 ISBN-13 (electronic): 978-1-4842-7999-1 https://doi.org/10.1007/978-1-4842-7999-1 Copyright © 2022 by Joshua M Willman This work is subject to copyright. All rights are reserved 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. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. 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. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Celestin Suresh John Development Editor: James Markham Coordinating Editor: Divya Modi Cover designed by eStudioCalamar Cover image designed by Pixabay Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at https://github.com/Apress/Beginning-PyQt-- second-edition. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper Joshua M Willman Sunnyvale, CA, USA
v Chapter 1: Getting Started with PyQt 1 The PyQt Framework ��������������������������������������������������������������������������������������������������������������������� 1 Why Choose PyQt? ������������������������������������������������������������������������������������������������������������������� 2 PyQt5 vs� PyQt6 ����������������������������������������������������������������������������������������������������������������������� 3 Installing Python 3 and PyQt6 ������������������������������������������������������������������������������������������������������� 3 Checking Your Version of Python ��������������������������������������������������������������������������������������������� 4 Installing PyQt6������������������������������������������������������������������������������������������������������������������������ 4 Introduction to User Interfaces ����������������������������������������������������������������������������������������������������� 5 What Is a Graphical User Interface? ���������������������������������������������������������������������������������������� 5 Concepts for Creating Good Interface Design �������������������������������������������������������������������������� 6 Creating Your First GUI: An Empty Window ������������������������������������������������������������������������������������ 7 Explanation for Creating an Empty Window ���������������������������������������������������������������������������� 8 Summary������������������������������������������������������������������������������������������������������������������������������������� 11 Chapter 2: Building a Simple GUI 13 The QLabel Widget ���������������������������������������������������������������������������������������������������������������������� 13 Explanation for Using QLabel ������������������������������������������������������������������������������������������������� 14 Project 2�1 – User Profile GUI ������������������������������������������������������������������������������������������������������ 18 Designing the User Profile GUI ����������������������������������������������������������������������������������������������� 19 Explanation for the User Profile GUI �������������������������������������������������������������������������������������� 20 Summary������������������������������������������������������������������������������������������������������������������������������������� 24 Table of Contents About the Author xiii About the Technical Reviewers xv Acknowledgments xvii Introduction xix
vi Chapter 3: Adding More Functionality with Widgets 25 Event Handlers and Signals and Slots ���������������������������������������������������������������������������������������� 25 The QPushButton Widget ������������������������������������������������������������������������������������������������������������ 26 Explanation for Using QPushButton ��������������������������������������������������������������������������������������� 27 The QLineEdit Widget ������������������������������������������������������������������������������������������������������������������ 31 Explanation for Using QLineEdit �������������������������������������������������������������������������������������������� 31 The QCheckBox Widget ��������������������������������������������������������������������������������������������������������������� 34 Explanation for Using QCheckBox ����������������������������������������������������������������������������������������� 35 The QMessageBox Dialog ����������������������������������������������������������������������������������������������������������� 38 Windows vs� Dialogs�������������������������������������������������������������������������������������������������������������� 39 Explanation for Using QMessageBox ������������������������������������������������������������������������������������� 39 Project 3�1 – Login GUI and Registration Dialog ������������������������������������������������������������������������� 44 Designing the Login GUI and Registration Dialog ������������������������������������������������������������������ 46 Explanation for Creating the Login GUI ���������������������������������������������������������������������������������� 47 Explanation for Creating the Main Window ��������������������������������������������������������������������������� 55 Explanation for Creating the Registration Dialog ������������������������������������������������������������������� 56 Summary������������������������������������������������������������������������������������������������������������������������������������� 61 Chapter 4: Learning About Layout Management 63 Using Layout Managers in PyQt �������������������������������������������������������������������������������������������������� 64 Absolute Positioning ������������������������������������������������������������������������������������������������������������������� 65 Horizontal and Vertical Layouts with Box Layouts ���������������������������������������������������������������������� 66 Explanation for QHBoxLayout ������������������������������������������������������������������������������������������������ 66 Explanation for QVBoxLayout ������������������������������������������������������������������������������������������������� 69 Creating Nested Layouts ������������������������������������������������������������������������������������������������������������� 73 Explanation for Nested Layouts ��������������������������������������������������������������������������������������������� 74 Arranging Widgets in Grids with QGridLayout ����������������������������������������������������������������������������� 79 Explanation for QGridLayout �������������������������������������������������������������������������������������������������� 80 Building Forms with QFormLayout ���������������������������������������������������������������������������������������������� 87 Explanation for QFormLayout ������������������������������������������������������������������������������������������������ 88 Managing Pages with QStackedLayout ��������������������������������������������������������������������������������������� 95 Explanation for QStackedLayout �������������������������������������������������������������������������������������������� 97 Table of ConTenTs
vii Additional Tips for Managing Space ����������������������������������������������������������������������������������������� 101 Explanation for Managing Space ����������������������������������������������������������������������������������������� 102 Summary����������������������������������������������������������������������������������������������������������������������������������� 104 Chapter 5: Menus, Toolbars, and More 105 Common Practices for Creating Menus ������������������������������������������������������������������������������������ 106 Creating a Simple Menu Bar ����������������������������������������������������������������������������������������������������� 106 Explanation for Creating a Menu Bar ����������������������������������������������������������������������������������� 107 Using Icons and the QIcon Class ����������������������������������������������������������������������������������������������� 111 Explanation for Using Icons ������������������������������������������������������������������������������������������������� 112 Setting the Central Widget ��������������������������������������������������������������������������������������������������� 115 Built-in Dialog Classes in PyQt �������������������������������������������������������������������������������������������������� 116 The QFileDialog Class ���������������������������������������������������������������������������������������������������������� 116 The QInputDialog Class �������������������������������������������������������������������������������������������������������� 117 The QFontDialog Class ��������������������������������������������������������������������������������������������������������� 118 The QColorDialog Class ������������������������������������������������������������������������������������������������������� 119 The About QMessageBox ����������������������������������������������������������������������������������������������������� 121 Project 5�1 – Rich Text Notepad GUI ������������������������������������������������������������������������������������������ 122 Designing the Rich Text Notepad GUI ���������������������������������������������������������������������������������� 123 Explanation for the Rich Text Notepad GUI �������������������������������������������������������������������������� 123 Expanding the Features in a Main Window ������������������������������������������������������������������������������� 133 Explanation for Expanding the Features ������������������������������������������������������������������������������ 134 Project 5�2 – Simple Photo Editor GUI ��������������������������������������������������������������������������������������� 142 Designing the Photo Editor GUI �������������������������������������������������������������������������������������������� 143 Explanation for the Photo Editor GUI������������������������������������������������������������������������������������ 143 Summary����������������������������������������������������������������������������������������������������������������������������������� 159 Chapter 6: Styling Your GUIs 161 What Are Styles in PyQt? ����������������������������������������������������������������������������������������������������������� 161 Changing the Default Style �������������������������������������������������������������������������������������������������� 162 Modifying Widget Appearances ������������������������������������������������������������������������������������������������� 163 Using HTML to Change the Look of Text ������������������������������������������������������������������������������ 164 Using Qt Style Sheets to Change the Look of Widgets �������������������������������������������������������� 167 Table of ConTenTs
viii Explanation for Using “Inline” Qt Style Sheets �������������������������������������������������������������������� 169 Explanation for Using “Embedded” Qt Style Sheets ������������������������������������������������������������ 172 Organizing Widgets with Containers and Tabs �������������������������������������������������������������������������� 175 The QRadioButton Widget ���������������������������������������������������������������������������������������������������� 176 The QGroupBox Class ���������������������������������������������������������������������������������������������������������� 176 The QTabWidget Class ��������������������������������������������������������������������������������������������������������� 177 Explanation for Using Containers and Tabs ������������������������������������������������������������������������� 177 Project 6�1 – Food Ordering GUI ������������������������������������������������������������������������������������������������ 182 Design the Food Ordering GUI ��������������������������������������������������������������������������������������������� 184 Explanation for the Food Ordering GUI �������������������������������������������������������������������������������� 185 CSS Properties Reference ��������������������������������������������������������������������������������������������������������� 198 Summary����������������������������������������������������������������������������������������������������������������������������������� 199 Chapter 7: Handling Events in PyQt 201 Event Handling in PyQt �������������������������������������������������������������������������������������������������������������� 201 Using Signals and Slots ������������������������������������������������������������������������������������������������������� 202 Using Event Handlers to Handle Events ������������������������������������������������������������������������������� 202 Difference Between Signals and Slots and Event Handlers ������������������������������������������������ 203 Handling Key Events ����������������������������������������������������������������������������������������������������������������� 203 Explanation for Handling Key Events ����������������������������������������������������������������������������������� 204 Handling Mouse Events������������������������������������������������������������������������������������������������������������� 205 Explanation for Handling Mouse Events ������������������������������������������������������������������������������ 207 Creating Custom Signals ����������������������������������������������������������������������������������������������������������� 211 Explanation for Creating Custom Signals ���������������������������������������������������������������������������� 212 Summary����������������������������������������������������������������������������������������������������������������������������������� 216 Chapter 8: Creating GUIs with Qt Designer 217 Getting Started with Qt Designer ���������������������������������������������������������������������������������������������� 218 Installing Qt Designer ���������������������������������������������������������������������������������������������������������� 218 Exploring Qt Designer’s User Interface �������������������������������������������������������������������������������� 219 Qt Designer’s Editing Modes ����������������������������������������������������������������������������������������������� 225 Creating an Application in Qt Designer ������������������������������������������������������������������������������������� 226 Project 8�1 – Keypad GUI ���������������������������������������������������������������������������������������������������������� 227 Table of ConTenTs
ix Explanation for the Keypad GUI ������������������������������������������������������������������������������������������� 228 Extra Tips for Using Qt Designer ����������������������������������������������������������������������������������������������� 254 Setting Up Main Windows and Menus ��������������������������������������������������������������������������������� 254 Displaying Images in Qt Designer ���������������������������������������������������������������������������������������� 257 Adding Style Sheets in Qt Designer ������������������������������������������������������������������������������������� 257 Summary����������������������������������������������������������������������������������������������������������������������������������� 258 Chapter 9: Working with the Clipboard 259 The QClipboard Class ���������������������������������������������������������������������������������������������������������������� 259 Explanation for Using QClipboard ���������������������������������������������������������������������������������������� 261 Project 9�1 – Sticky Notes GUI �������������������������������������������������������������������������������������������������� 265 Explanation for the Sticky Notes GUI ����������������������������������������������������������������������������������� 267 Summary����������������������������������������������������������������������������������������������������������������������������������� 272 Chapter 10: Presenting Data in PyQt 273 Quickly Handling Data in PyQt��������������������������������������������������������������������������������������������������� 273 The QListWidget Class �������������������������������������������������������������������������������������������������������������� 274 Explanation for Using QListWidget �������������������������������������������������������������������������������������� 275 Drag and Drop in PyQt ��������������������������������������������������������������������������������������������������������������� 279 Explanation for Drag and Drop �������������������������������������������������������������������������������������������� 280 The QTableWidget Class ������������������������������������������������������������������������������������������������������������ 283 Explanation for Using QTableWidget ������������������������������������������������������������������������������������ 284 The QTreeWidget Class ������������������������������������������������������������������������������������������������������������� 293 Explanation for Using QTreeWidget ������������������������������������������������������������������������������������� 294 Summary����������������������������������������������������������������������������������������������������������������������������������� 297 Chapter 11: Graphics and Animation in PyQt 299 Introduction to the QPainter Class �������������������������������������������������������������������������������������������� 300 Explanation for Using the QPainter Class ���������������������������������������������������������������������������� 301 Project 11�1 – Painter GUI ��������������������������������������������������������������������������������������������������������� 314 Explanation for the Painter GUI ������������������������������������������������������������������������������������������������� 315 Creating the Canvas Class ��������������������������������������������������������������������������������������������������� 316 Creating the Painter GUI’s MainWindow Class �������������������������������������������������������������������� 322 Table of ConTenTs
x Animating Scenes with QPropertyAnimation ���������������������������������������������������������������������������� 327 Explanation for Animating Scenes �������������������������������������������������������������������������������������������� 329 Introduction to Animating Widgets �������������������������������������������������������������������������������������������� 334 Explanation for Animating Widgets �������������������������������������������������������������������������������������� 335 Summary����������������������������������������������������������������������������������������������������������������������������������� 340 Chapter 12: Creating Custom Widgets 341 Project 12�1 – RGB Slider Custom Widget ��������������������������������������������������������������������������������� 341 PyQt’s Image Handling Classes ������������������������������������������������������������������������������������������� 343 The QSlider Widget �������������������������������������������������������������������������������������������������������������� 344 Explanation for the RGB Slider Widget �������������������������������������������������������������������������������� 345 RGB Slider Demo ����������������������������������������������������������������������������������������������������������������������� 356 Explanation for the RGB Slider Demo ���������������������������������������������������������������������������������� 357 Summary����������������������������������������������������������������������������������������������������������������������������������� 358 Chapter 13: Working with Qt Quick 359 Outlining QtQuick and QML ������������������������������������������������������������������������������������������������������� 360 Elements in QtQuick ������������������������������������������������������������������������������������������������������������ 362 Introduction to the QML Language and Syntax ������������������������������������������������������������������� 363 Building and Running QML Components ����������������������������������������������������������������������������������� 366 Creating and Loading QML Components ����������������������������������������������������������������������������� 367 Creating Reusable Components ������������������������������������������������������������������������������������������ 373 Layout Handling in QML ������������������������������������������������������������������������������������������������������� 376 Building and Loading QML Windows ����������������������������������������������������������������������������������� 380 Using Transformations to Animate Objects ������������������������������������������������������������������������������� 387 Explanation for Simple Transformations ������������������������������������������������������������������������������ 388 Explanation for Using Transformations to Animate Objects ������������������������������������������������� 390 Summary����������������������������������������������������������������������������������������������������������������������������������� 394 Chapter 14: Introduction to Handling Databases 395 Thinking About Data ������������������������������������������������������������������������������������������������������������������ 395 Introduction to Model/View Programming �������������������������������������������������������������������������������� 396 Table of ConTenTs
xi The Components of the Model/View Architecture ��������������������������������������������������������������� 396 PyQt’s Model/View Classes ������������������������������������������������������������������������������������������������� 398 Explanation for Introduction to Model/View ������������������������������������������������������������������������ 400 Working with SQL Databases in PyQt ���������������������������������������������������������������������������������������� 404 What Is SQL? ����������������������������������������������������������������������������������������������������������������������� 404 Project 14�1 – Account Management GUI ��������������������������������������������������������������������������������� 407 Explanation for Working with the QtSql Module ������������������������������������������������������������������ 409 Explanation for Querying a Database with QSqlQuery �������������������������������������������������������� 416 Working with the QSqlTableModel Class ����������������������������������������������������������������������������� 419 Working with the QSqlRelationalTableModel Class ������������������������������������������������������������� 423 Explanation for the Account Management GUI �������������������������������������������������������������������� 428 Summary����������������������������������������������������������������������������������������������������������������������������������� 436 Chapter 15: Managing Threads 437 Introduction to Threading ���������������������������������������������������������������������������������������������������������� 437 Threading in PyQt ���������������������������������������������������������������������������������������������������������������� 438 Methods for Processing Long Events in PyQt ���������������������������������������������������������������������� 439 Project 15�1 – File Renaming GUI ��������������������������������������������������������������������������������������������� 440 The QProgressBar Widget ���������������������������������������������������������������������������������������������������� 441 Explanation for File Renaming GUI �������������������������������������������������������������������������������������� 441 Summary����������������������������������������������������������������������������������������������������������������������������������� 450 Chapter 16: Extra Projects 451 Project 16�1 – Directory Viewer GUI ������������������������������������������������������������������������������������������ 452 Explanation for the Directory Viewer GUI ����������������������������������������������������������������������������� 453 Project 16�2 – Camera GUI �������������������������������������������������������������������������������������������������������� 456 Explanation for the Camera GUI ������������������������������������������������������������������������������������������� 458 Project 16�3 – Simple Clock GUI ����������������������������������������������������������������������������������������������� 464 Explanation for the Clock GUI ���������������������������������������������������������������������������������������������� 465 Project 16�4 – Calendar GUI ������������������������������������������������������������������������������������������������������ 467 Explanation for the Calendar GUI ����������������������������������������������������������������������������������������� 468 Project 16�5 – Hangman GUI ����������������������������������������������������������������������������������������������������� 473 Explanation for the Hangman GUI ���������������������������������������������������������������������������������������� 475 Table of ConTenTs
xii Project 16�6 – Web Browser GUI ����������������������������������������������������������������������������������������������� 484 Explanation for Web Browser GUI ���������������������������������������������������������������������������������������� 486 Project 16�7 – Tri-state QComboBox ����������������������������������������������������������������������������������������� 497 Explanation for the Tri-state QComboBox ��������������������������������������������������������������������������������� 498 Summary����������������������������������������������������������������������������������������������������������������������������������� 501 Appendix: Reference Guide for PyQt6 503 Selected PyQt6 Modules ����������������������������������������������������������������������������������������������������������� 503 Selected PyQt Classes �������������������������������������������������������������������������������������������������������������� 504 Classes for Building a GUI Window �������������������������������������������������������������������������������������� 505 QPainter ������������������������������������������������������������������������������������������������������������������������������� 509 Layout Managers ����������������������������������������������������������������������������������������������������������������� 511 Button Widgets �������������������������������������������������������������������������������������������������������������������� 512 Input Widgets ����������������������������������������������������������������������������������������������������������������������� 514 Display Widgets ������������������������������������������������������������������������������������������������������������������� 522 Item Views ��������������������������������������������������������������������������������������������������������������������������� 524 Container Widgets ��������������������������������������������������������������������������������������������������������������� 526 QtQuick and QML ���������������������������������������������������������������������������������������������������������������������� 529 Qt Style Sheets �������������������������������������������������������������������������������������������������������������������������� 529 Qt Namespace ��������������������������������������������������������������������������������������������������������������������������� 532 Summary����������������������������������������������������������������������������������������������������������������������������������� 532 Index 533 Table of ConTenTs
xiii 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
xv 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.
xvii 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.
xix 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.
xx 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. InTroduCTIon
xxi 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: Listing 1. Setting up the main window to show how to use QLabel widgets # labels.py The second part of the application is listed as Listing 2. The setUpMainWindow() method for displaying text and image labels # labels.py 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. InTroduCTIon
xxii 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 redhuli.comments@gmail.com. InTroduCTIon
1 © Joshua M Willman 2022 J. M. Willman, Beginning PyQt, https://doi.org/10.1007/978-1-4842-7999-1_1 CHAPTER 1 Getting Started with PyQt Hello! Welcome to Beginning PyQt: A Hands-on Approach to GUI Programming. You are probably here because you would like to learn how to build applications and need some help getting started. Maybe you have a personal program you need to build, or perhaps you would like to build a custom software application for others to use. Whatever your reason may be, you’ll need to figure out where is the best place to start. The goal of this book is to take a practical approach to coding user interfaces. You’ll be able to follow along and code numerous examples, both simple and complex. You’ll also gain knowledge through visualization and practice that show how to use fundamental concepts you need to build your own applications. In many cases, those concepts will then be applied to larger projects. In this chapter, you will • Learn about the PyQt toolkit for creating user interfaces • Set up Python 3 and download the latest version of PyQt6 • Consider some fundamental concepts for building user-friendly interfaces • Create your first application with PyQt Let’s begin this journey by finding out more about PyQt. The PyQt Framework The PyQt toolkit is a set of Python bindings for the Qt cross-platform widget toolkit and application framework. What does that mean? First, Qt is used for the development of user interfaces and other applications and is being developed by The Qt Company. The framework is significant because it can run on numerous software and hardware systems such as Windows, macOS, Linux, Android, or embedded systems with little to no change to the underlying code and is still able to
Comments 0
Loading comments...
Reply to Comment
Edit Comment