PyQt6 101: A Beginner’s guide to PyQt6
By Edward Chang
()
About this ebook
This book presents a concise, accessible introduction to PyQt6. It will: show you the rungs using simple, up-to-date scripts, explain all the basics you need to get started, avoid all the dull theory, and, slowly guide you to more advanced scripts. It is perfect for the beginner who just wants to dabble and will serve as a motivator for further exploration for those interested in longer term use of PyQt6.
Related to PyQt6 101
Related ebooks
JavaScript : Object-Oriented Programming Rating: 0 out of 5 stars0 ratingsPuppet 5 Beginner's Guide - Third Edition: Go from newbie to pro with Puppet 5 Rating: 4 out of 5 stars4/5Extending Puppet - Second Edition Rating: 0 out of 5 stars0 ratingsPuppet 4.10 Beginner’s Guide - Second Edition Rating: 0 out of 5 stars0 ratingsExpert Python Programming - Second Edition Rating: 2 out of 5 stars2/5Python: Journey from Novice to Expert Rating: 0 out of 5 stars0 ratingsNode.js Web Development - Third Edition Rating: 2 out of 5 stars2/5React: Building Modern Web Applications Rating: 0 out of 5 stars0 ratingsPython: Master the Art of Design Patterns Rating: 0 out of 5 stars0 ratingsMastering Python Rating: 0 out of 5 stars0 ratingsModular Programming with JavaScript Rating: 0 out of 5 stars0 ratingsqooxdoo Beginner's Guide Rating: 0 out of 5 stars0 ratingsNode.js Design Patterns - Second Edition Rating: 4 out of 5 stars4/5Python Essentials Rating: 5 out of 5 stars5/5Software Architecture with Python Rating: 0 out of 5 stars0 ratingsJavaScript for .NET Developers Rating: 0 out of 5 stars0 ratingsElements of Android Room Rating: 0 out of 5 stars0 ratingsPractical Maya Programming with Python Rating: 0 out of 5 stars0 ratingsPython: Real-World Data Science Rating: 0 out of 5 stars0 ratingsMastering KnockoutJS Rating: 0 out of 5 stars0 ratingsNative Docker Clustering with Swarm: Create and manage clusters of any size Rating: 0 out of 5 stars0 ratingsOpenStack Essentials - Second Edition Rating: 0 out of 5 stars0 ratingsAndroid Programming for Beginners Rating: 3 out of 5 stars3/5Mastering Go Network Automation: Automating Networks, Container Orchestration, Kubernetes with Puppet, Vegeta and Apache JMeter Rating: 0 out of 5 stars0 ratingsMastering Go Network Automation Rating: 0 out of 5 stars0 ratingsTroubleshooting Docker Rating: 0 out of 5 stars0 ratingsMastering Swift Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5The Most Concise Step-By-Step Guide To ChatGPT Ever Rating: 3 out of 5 stars3/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsPython QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsHTML in 30 Pages Rating: 5 out of 5 stars5/5Mastering Excel: Starter Set Rating: 2 out of 5 stars2/5Coding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratingsSpies, Lies, and Algorithms: The History and Future of American Intelligence Rating: 4 out of 5 stars4/5C Programming For Beginners: The Simple Guide to Learning C Programming Language Fast! Rating: 5 out of 5 stars5/5A Slackers Guide to Coding with Python: Ultimate Beginners Guide to Learning Python Quick Rating: 0 out of 5 stars0 ratings
Reviews for PyQt6 101
0 ratings0 reviews
Book preview
PyQt6 101 - Edward Chang
PyQt6 101
A Beginner’s guide to PyQt6
Edward Chang
Text copyright © 2024 by Edward Chang
All rights reserved. Without limiting the rights under the copyright reserved above, no part of this publication may be reproduced, stored in, or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise) without prior written permission.
For permission requests, please contact: @thehackeruni
ISBN-13: 9798224056675
Table of Contents
Preface to the 2nd Edition Book source code Preface to the 1st Edition Conventions used Book source code Images 1. Object-oriented Programming Refresher Why OOP? Classes Class methods Object initialisation What is the nature of self? Inheritance Story time Back to class Overriding Multiple Inheritance super()-Man Summary References 2. Your first PyQt programs First example Second example Summary 3. Layout management in PyQt Absolute positioning Layout classes Box layout Grid layout Form layout Summary References 4. Signals and slots Custom slot Using a provided slot A typical example Summary References 5. PyQt widgets QLineEdit QCalendarWidget QCheckBox QSlider QProgressBar QOpenGLWidget Miscellaneous widgets QComboBox QMovie QLabel Summary References 6. FileLister Qt Creator Summary 7. Currency Exchange Rates App Designing the UI Switching back to Python Web service communication Putting it all together Summary References 8. PyQt databases and CSS styling Qt Databases Connecting to the database and querying it Models QsqlQueryModel QsqlTableModel QsqlRelationalTableModel QSqlRelationalDelegate CSS Styling Style Rules Selector Types Sub-Controls Pseudo-States Conflict Resolution Cascading Inheritance Example Summary References 9. Radio App Setting up Gstreamer Pipeline Elements Pads Element states Bringing it all together Explanation of the code Radio app Summary References A. Multi-threading How to implement threading References B. Network Access Manager References C. Using Docker Initial setup Windows Other operating systems D. Python’s __name__ variable
Preface to the 2nd Edition
Thanks to the support of my readers, I’m back with the 2nd edition of this book.
This second edition attempts to bring the original up to date by thorough revision, and, addition of a new chapter that reviews Object-oriented Programming(OOP). Also, running some of the apps in the book has been problematic for some users because of the app dependencies. A Dockerfile that runs all the apps in this book and installs all the required dependencies has been provided.
Book source code
Source code for the book can be found at:
https://gitlab.com/wohlstetter/pyqt6-101-2ndedition
Preface to the 1st Edition
I could bore you with a lot of theory about Python, the history of user interface (U.I.) design and so on. But, I trust that you’ll be able to research all the dreary stuff when(if ever!) you need it. Instead, like Neo in the Matrix, we’ll skip all the boring stuff and get right to the meat and potatoes of the subject.
The only assumptions I make are:
That you’re acquainted with the basics of Python. You can always refer to:
https://docs.python.org/3/tutorial/
if you’re a bit rusty.
Some experience using the command line interface (CLI) on your operating system.
Know how to install python modules.
Basic knowledge of the HTTP protocol.
Basic knowledge of MySQL databases. A brief overview can be found at:
https://www.tutorialspoint.com/sql/sql-overview.htm
A beautifully designed e-book with more details can be found at:
https://goalkicker.com/SQLBook/
It also helps to have experience with a visual
programming language like Visual Basic, Delphi or mobile phone programming. But, it’s not necessary.
Conventions used
There are a number of text conventions used throughout this book.
Italics: Used for a new term, an important word, or words, function names, variable names, command-line input and code in text. Here is an example:
Change the button from its default name pushButton to btnBrowse.
When we wish to draw your attention to a particular part of code or output, the relevant lines or items are set in bold: Review the last minute of the video to see this!
Book source code
Source code for the book can be found at:
https://gitlab.com/wohlstetter/pyqt6-101
Images
Some images had to be shrank so they could be displayed in the document. The most important of these can be found in the images folder of the source code.
As always, happy coding!
Chapter 1. Object-oriented Programming Refresher
This chapter is mainly intended for beginner Python coders. Experienced Python coders may skip it.
I will not exhaustively cover Object-oriented Programming(OOP). I’ll only go over the ideas you need to understand the scripts in this book.
If you want to follow along, you can create a new text file and paste in the following code. Else, this and all other code will be found in the source code that came with your book. The code for each chapter is in a folder named after that chapter.
Why OOP?
Before OOP, there was procedural programming that divided a program into functions and data that these functions manipulated. A program was like a recipe that sequentially manipulated data to produce the result we wanted.
But as programs grew larger, the overlap between functions and their data often caused unforeseen problems. OOP broke down unwieldy programs into smaller units of related variables and functions called objects. We call these variables attributes and the functions that manipulate them are called methods.
Classes
A class is a representation of an object. It is the blueprint from which individual objects are created.
A simple example of a class can be found in basicexample.py in the folder for this chapter.
1 class
exampleClass:
2
A simple example of a class.
3 i =
5000
To create an instance of a class, you use function notation. The following creates an instance of exampleClass and returns an object that we assign to a variable named y.
y = exampleClass()
Our object has two attributes: an integer and a docstring which we can access as shown below.
>>> y = exampleClass()
>>>
>>> y.i
5000
>>>
>>> y.__doc__
' A simple example of a class. '
>>>
Class methods
Methods are functions within a class that are designed to perform a particular task. Python distinguishes between user-defined methods written by a programmer and special methods that are part of the language.
These magic methods have a double underscore(dunder) on either side of their name for example: __init__() .
Object initialisation
Instantiation i.e. calling a class object gives us an empty object. Setting the initial state of objects in Java and C++ is done using a constructor method. The Python equivalent of this is the __new__() method.
To see a demonstration of this, open the file called classes101.py in a terminal. One way to do this is by typing the following:
python3 -i classes101.py
The code of class classDemo is shown below.
1 class
classDemo:
2
def
__new__(cls, *args, **kwargs):
3
print ('__new__ is the constructor for a class'
)
4
return
object.__new__(cls)
5
6
def
__init__(self, args):
7
print('__init__ is the initialiser for a class'
)
8
9
def
__del__(self):
10
print('__del__ is the finaliser for a class'
)
11
12
def
greeting(self):
13
print('__new__ or __init__? Now you know what get\'s called first :)')
When we create an object called x as shown, we get the following:
>>> x = classDemo('')
__new__ is the constructor for a class
__init__ is the initialiser for a class
__new__() is rarely overridden[1] in Python, so we don’t need to worry about it at this stage. We’ll do all our