Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

PyQt6 101: A Beginner’s guide to PyQt6
PyQt6 101: A Beginner’s guide to PyQt6
PyQt6 101: A Beginner’s guide to PyQt6
Ebook272 pages1 hour

PyQt6 101: A Beginner’s guide to PyQt6

Rating: 0 out of 5 stars

()

Read preview

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.

LanguageEnglish
PublisherEdward Chang
Release dateJul 23, 2024
ISBN9798224056675
PyQt6 101: A Beginner’s guide to PyQt6

Related to PyQt6 101

Related ebooks

Programming For You

View More

Related articles

Reviews for PyQt6 101

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    PyQt6 101 - Edward Chang

    Title page

    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

    Enjoying the preview?
    Page 1 of 1