Mastering GUI Programming with Python: Develop impressive cross-platform GUI applications with PyQt
Book information
Description
An advanced guide to creating powerful high-performance GUIs for modern, media-rich applications in various domains such as business and game development Key Features Gain comprehensive knowledge of Python GUI development using PyQt 5.12 Explore advanced topics including multithreaded programming, 3D animation, and SQL databases Build cross-platform GUIs for Windows, macOS, Linux, and Raspberry Pi Book Description PyQt5 has long been the most powerful and comprehensive GUI framework available for Python, yet there is a lack of cohesive resources available to teach Python programmers how to use it. This book aims to remedy the problem by providing comprehensive coverage of GUI development with PyQt5. You will get started with an introduction to PyQt5, before going on to develop stunning GUIs with modern features. You will then learn how to build forms using QWidgets and learn about important aspects of GUI development such as layouts, size policies, and event-driven programming. Moving ahead, you'll discover PyQt5's most powerful features through chapters on audio-visual programming with QtMultimedia, database-driven software with QtSQL, and web browsing with QtWebEngine. Next, in-depth coverage of multithreading and asynchronous programming will help you run tasks asynchronously and build high-concurrency processes with ease. In later chapters, you'll gain insights into QOpenGLWidget, along with mastering techniques for creating 2D graphics with QPainter. You'll also explore PyQt on a Raspberry Pi and interface it with remote systems using QtNetwork. Finally, you will learn how to distribute your applications using setuptools and PyInstaller. By the end of this book, you will have the skills you need to develop robust GUI applications using PyQt. What you will learn Get to grips with the inner workings of PyQt5 Learn how elements in a GUI application communicate with signals and slots Learn techniques for styling an application Explore database-driven applications with the QtSQL module Create 2D graphics with QPainter Delve into 3D graphics with QOpenGLWidget Build network and web-aware applications with QtNetwork and QtWebEngine Who this book is for This book is for programmers who want to create attractive, functional, and powerful GUIs using the Python language. You'll also find this book useful if you are a student, professional, or anyone who wants to start exploring GUIs or take your skills to the next level. Although prior knowledge of the Python language is assumed, experience with PyQt, Qt, or GUI programming is not required. Table of Contents Getting Started with PyQt Building Forms with QtWidgets Handling events with signals and slots Building applications with QMainWindow Creating data interfaces with Model/View classes Styling Qt Applications Working with Audio-visual Using QtMultimedia Networking with QtNetwork Exploring SQL with QtSQL Multithreading with QTimer and Qthread Creating Rich text with QTextDocument Creating 2D graphics with Qpainter Creating 3D graphics with QtOpenGL Embedding data plots with QtCharts PyQt Raspberry Pi Web browsing with QtWebEngine Preparing Your Software for Distribution Appendix A: Answers to Questions Appendix B: Upgrading Raspbian 9 to Raspbian 10 Cover Title Page Copyright and Credits Dedication About Packt Contributors Table of Contents Preface Section 1: Deep Dive into PyQt Chapter 1: Getting Started with PyQt Technical requirements Installing Qt Designer Introducing Qt and PyQt PyQt5 Working with Qt and PyQt Understanding Qt's documentation Core Qt modules Creating Hello Qt – our first window Creating a PyQt application template Introducing Qt Designer Using Qt Designer Summary Questions Further reading Chapter 2: Building Forms with QtWidgets Technical requirements Creating basic QtWidgets widgets QWidget QWidget as a top-level window QLabel QLineEdit QPushButton and other buttons QComboBox QSpinBox QDateTimeEdit QTextEdit Placing and arranging widgets Layout classes QHBoxLayout and QVBoxLayout QGridLayout QFormLayout Controlling widget size Container widgets QTabWidget QGroupBox Validating widgets Creating an IPv4 entry widget Using QSpinBox for discrete values Building a calendar application GUI Building the GUI in code Creating the widgets Building the layout Building the GUI in Qt Designer First steps Building the right panel Building the event form Previewing the form Summary Questions Further reading Chapter 3: Handling Events with Signals and Slots Technical requirements Signal and slot basics Restrictions on signal and slot connections Creating custom signals and slots Sharing data between windows using custom signals Overloading signals and slots Automating our calendar form Using our hand-coded form Creating and connecting our callback methods The populate _list () method The populate _form () method The save _event () method The delete _event () method The check _delete _btn () method Building our new category pop-up form Using Qt Designer .ui files Connecting slots in Qt Designer Converting .ui files to Python Automatic signal and slot connections Using .ui files without conversion Summary Questions Further reading Chapter 4: Building Applications with QMainWindow Technical requirements The QMainWindow class Setting a central widget Adding a status bar Creating an application menu Menus on macOS Adding toolbars Adding dock widgets Other QMainWindow features Standard dialog boxes QMessageBox QFileDialog QFontDialog Other dialog boxes Saving settings with QSettings Limitations of QSettings Summary Questions Further reading Chapter 5: Creating Data Interfaces with Model-View Classes Technical requirements Understanding model-view design Models and views in PyQt Building a CSV editor Creating a table model Implementing read capabilities Adding headers and sorting Implementing write capabilities Using the model in a view Summary Questions Further reading Chapter 6: Styling Qt Applications Technical requirements Using fonts, images, and icons Setting a font Dealing with missing fonts Adding images Using icons Using Qt resource files Qt resource files and fonts Configuring colors, style sheets, and styles Customizing colors with palettes Working with QBrush objects Customizing the appearance with Qt Style Sheets (QSS) The downside of QSS Customizing the appearance with QStyle Customizing Qt styles Drawing widgets Creating animations Basic property animations Animating colors Using animation groups Summary Questions Further reading Section 2: Working with External Resources Chapter 7: Working with Audio-Visual Using QtMultimedia Technical requirements Simple audio playback Recording and playing audio The initial setup Implementing sound playback Loading the media Tracking the playback position Looping the audio Setting the volume Implementing recording Examining and configuring the recorder Recording and playing video Building the basic GUI Video playback Video recording Summary Questions Further reading Chapter 8: Networking with QtNetwork Technical requirements Low-level networking with sockets Building a chat GUI Building a UDP chat client Connecting signals Testing the chat Building a TCP chat client Working with data streams Sending data over TCP Connecting our backend and testing HTTP communications with QNetworkAccessManager Simple downloading Posting data and files Building the GUI The POSTing backend Testing the utility Summary Questions Further reading Chapter 9: Exploring SQL with Qt SQL Technical requirements SQL basics Creating tables Inserting and updating data Updating existing rows Selecting data Table joins SQL subqueries Learning more Performing SQL queries with Qt Building a form Connecting and making simple queries Getting information about the database Making simple queries Prepared queries Using QSqlQueryModel Finishing the GUI Using model-view widgets without SQL Delegates and data mapping Data mapping Filtering data Using a custom delegate Inserting custom rows in a table view Summary Questions Further reading Section 3: Unraveling Advanced Qt Implementations Chapter 10: Multithreading with QTimer and QThread Technical requirements Delayed actions with QTimer Single shot timers Repeating timers Getting information from timers Limitations of timers Multithreading with QThread The SlowSearcher file search engine A non-threaded searcher Testing our non-threaded search application Adding threads An alternate method Threading tips and caveats High concurrency with QThreadPool and QRunner The file hasher GUI A hash runner Creating the thread pool Testing the script Threading and the Python GIL Summary Questions Further reading Chapter 11: Creating Rich Text with QTextDocument Technical requirements Creating rich text using markup HTML basics Style sheet syntax Semantic versus cosmetic tags Structure and heading tags Typography tags Hyperlinks Lists and tables Fonts, colors, images, and styles Document-wide styles Images Differences between Qt rich text and Web HTML Manipulating rich text using QTextDocument Creating the invoice application GUI Building InvoiceView The QTextDocument structure Character formats Adding basic content Inserting a list Inserting a table Finishing and testing Printing rich text Updating the Invoice app for print support Configuring the printer Printing a page Print previewing Exporting to PDF Summary Questions Further reading Chapter 12: Creating 2D Graphics with QPainter Technical requirements Image editing with QPainter The meme generator GUI The editing form The main GUI Drawing with QImage The QPainter object Saving our image Custom widgets with QPainter Building a GraphWidget Painting the widget Using GraphWidget Animating 2D graphics with QGraphicsScene First steps Making a scene Creating the tanks Creating the bullets Collision detection Finishing the game Summary Questions Further reading Chapter 13: Creating 3D Graphics with QtOpenGL Technical requirements The basics of OpenGL The rendering pipeline and drawing basics Programs and shaders A simple vertex shader A simple fragment shader Embedding OpenGL drawings with QOpenGLWidget First steps with OpenGLWidget Creating a program Accessing our variables Configuring a projection matrix Drawing our first shape Creating a 3D object Animating and controlling OpenGL drawings Animating in OpenGL Zooming in and out Summary Questions Further reading Chapter 14: Embedding Data Plots with QtCharts Technical requirements Making a simple chart Setting up the GUI Building a disk usage chart Displaying real-time data Building a CPU usage chart Updating the chart data Panning and zooming around the chart Styling Qt charts Building the memory chart Chart styling Styling axes Styling the legend Summary Questions Further reading Chapter 15: PyQt Raspberry Pi Technical requirements Running PyQt5 on the Pi Editing Python on the Pi Running PyQt5 applications on the Pi Controlling GPIO devices with PyQt Connecting the LED circuit Writing a driver library PWM Setting a color Cleaning up Creating the PyQt GUI Controlling PyQt with GPIO devices Connecting the sensor circuit Creating the sensor interface Displaying the readings Adding a hardware button Expanding the circuit Implementing the button driver Summary Questions Further reading Chapter 16: Web Browsing with QtWebEngine Technical requirements Building a basic browser with QWebEngineView Using the QWebEngineView widget Allowing multiple windows and tabs Adding a tab for pop-up windows Advanced QtWebEngine usage Sharing a profile Viewing history Web settings Building a text search feature Summary Questions Further reading Chapter 17: Preparing Your Software for Distribution Technical requirements Structuring a project Tic-tac-toe The engine class The board class The main window class Module-style structure Structuring the module Non-Python files Documentation and metadata The LICENSE file The README file The docs directory The requirements.txt file Other files Distributing with setuptools Writing the setuptools configuration Basic metadata arguments Packages and dependencies Non-Python files Executables Source distributions Creating a source distribution Installing a source distribution Built distributions Types of built distributions Creating a wheel distribution Installing a built distribution Compiling with PyInstaller PyInstaller overview Basic command-line usage The .spec file Preparing QTicTacToe for PyInstaller Dealing with non-Python files Further debugging Summary Questions Further reading Appendix A: Answers to Questions Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Appendix B: Upgrading Raspbian 9 to Raspbian 10 Other Books You May Enjoy Index
Similar books
Python GUI Programming with Tkinter: Design and build functional and user-friendly GUI applications, 2nd Edition
2021 · PDF
Python GUI Programming with Tkinter: Design and build functional and user-friendly GUI applications, 2nd Edition
2021 · PDF
Python GUI Programming with Tkinter: Design and build functional and user-friendly GUI applications, 2nd Edition
2021 · EPUB
Mastering GUI Programming with Python
2019 · EPUB
Python GUI programming with Tkinter
2018 · PDF
MySQL® Notes for Professionals book
2018 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF