© Joshua M. Willman 2020
J. M. WillmanBeginning PyQthttps://doi.org/10.1007/978-1-4842-5857-6_1

1. Charting the Course

Joshua M. Willman1 
(1)
Hampton, VA, USA
 

Hello! Welcome to Beginning PyQt: A Hands-on Approach to GUI Programming. The goal of this book is to take a more practical approach to learning how to code user interfaces (UIs), following along and coding numerous examples, both simple and complex, to help understand and visualize how we can use the concepts taught in each chapter. What that means is that when you learn how to code QPushButton widgets, for example, you will first walk through a simple program that helps you build the fundamentals. Then, you will apply that concept to a slightly larger project.

“When am I ever going to use this?” I can still recall sitting in my math classes and hearing someone ask that question. The formulas and theories, culminations of numerous mathematicians’ life’s work, were all amazing to learn, but without some way to apply them to actual examples beyond the textbook, those concepts faded away into some dark recess in my mind.

To avoid spiraling down this same path when learning to code, this book aims to help you jump right into actual examples to get you coding and practicing the concepts with a hands-on approach. New concepts and PyQt classes are introduced in each chapter, and later chapters sometimes build upon the previous ones. Of course, not everyone has the same goal in mind. Therefore, there are a couple of ways to approach the content of this book. The first way is for readers who want to follow along and practice learning many of the basics of PyQt. These types of readers are encouraged to code many of the projects and then play around with the concepts to design their own applications. The other approach is for those readers who already have a project in mind and need some help getting started. You are definitely encouraged to use the code in this book as a foundation to build your own projects and get them off the ground.

Who Should Read This Book

Everyone must begin somewhere. With that idea in mind, this book is targeted for individuals who already have a fundamental understanding of the Python programming language and are looking to either expand their skills in Python or have a project where they need to create a UI, but may have no prior experience creating UI or no idea where to begin. Having prior knowledge of other Python UI toolkits is not necessary to get started in this book.

Introduction to User Interfaces

The user interface (UI) has become a key component of our everyday lives, becoming the intermediary between us and our ever-growing number of machines. A UI is designed to facilitate in human-computer interaction. The human needs to operate and control the machine to serve some purpose; meanwhile the machine needs to simultaneously provide feedback to aid the human’s decision-making process. UIs are everywhere, from the mobile applications on our phones to web browsers, to heavy machinery controls, and even on the appliances in our kitchens. Of course, the ways in which we interact with technology are not merely limited to our hands, as many UIs also allow interaction with our other sensory organs.

A good UI is tasked with helping a person produce a desired result while also allowing for easier, more efficient, and more friendly operation of a machine. Think about the photo editing apps on your phone. Editing the size, color, or exposure is practically effortless as you slide your fingers across the screen and watch the images change almost instantly. The user provides minimal input to achieve the desired output.

What Is a Graphical User Interface?

For this book, we will be focusing on creating graphical user interfaces (GUIs) which take advantage of a computer’s graphics capabilities to create visual controls on a machine’s screen. This makes interaction with machines much easier. Decades ago, users would have to use the command line and text commands to interact with the computer. Tasks such as opening, deleting, and moving files or searching through directories were all done by typing in certain commands. However, these were not very user friendly or simple to use. So GUIs were created to allow users to interact with electronic devices using graphical controls, rather than command-line interfaces.

These graphical control elements, or widgets, such as buttons, menus, and windows, make such tasks effortless. Interaction now becomes as simple as moving your mouse or touching the screen depending upon your device and clicking the widget.

Concepts for Creating Good Interface Design

This, first and foremost, is a technical book written to help those of you who want to learn how to create and code your own GUI with PyQt and Python. That being said, if you plan to design any kind of UI that other people will use, then you are no longer creating a UI just to solve some problem. You must also begin to consider other users of the application, as well. Think about what you want them to accomplish, or how the application can help them. Sometimes when we are trying to solve a problem, we get so caught up in trying to create a product that we forget about the people who actually have to interact with them.

The following are a list of guidelines to consider when designing your own UI. They are not set rules and by no means a complete list, but rather ideas that can help to save you some time and headaches later.

  1. 1.

    Clarity – Using clear language, hierarchy, and flow with visual elements to avoid ambiguity. One of the ways this can be achieved is by considering visual importance to the human eye, laying out widgets with bigger sizes, darker colors, and so on in such a manner that we can visually understand the UI.

     
  2. 2.

    Conciseness  – Simplifying the layout to include only what the user needs to see or interact with at a given time in order to be brief, but also comprehensive. Adding more labels or buttons in your window just to give the user more options is not always better.

     
  3. 3.

    Consistency – Design the UI so that there is consistency across the application. This helps users to recognize patterns in the visual elements and layout and can be seen in typography that improves the navigation and readability of the application, image styles, or even color schemes.

     
  4. 4.

    Efficiency – Utilizing good design and shortcuts to help the user improve productivity. If a task can be accomplished in two steps, why design it so that it has to be completed in five?

     
  5. 5.

    Familiarity – Consider elements that users normally see in other UIs and how they would expect them to perform in your applications. For example, think about how weird it would be to have to enter your login information and the password entry field is above the username. It is not wrong, but now you are unnecessarily making users think about their actions and slowing them down.

     
  6. 6.

    Responsive  – Give the user feedback, for example, a toggle that changes color to “on” or “off,” a small message to notify the user if their input is correct or incorrect, or even a sound effect to verify a completed action. The user should never be left wondering if their action was successful or not.

     

The PyQt Framework

The PyQt application is a set of Python 2 and Python 3 bindings for the Qt cross-platform widget toolkit and application framework. What does that mean?

First, Qt is used for the development of graphical user interfaces and other applications and is currently 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 maintain capabilities and speed of the system on which it is being run.

Second, this all means that PyQt combines all the advantages of the Qt C++ cross-platform widget toolkit with Python, the powerful and simple, cross-platform interpreted language.

For more information about PyQt, check out

www.riverbankcomputing.com/news.

Why Choose PyQt?

PyQt is capable of more than just creating GUIs, as it also has access to Qt classes that cover mechanics such as XML handling, SQL databases, network communication, graphics and animations, and many other technologies. Take the capabilities of Qt and combine it with the number of extension modules that Python provides, and you have the ability to create new applications that can build upon these preexisting libraries.

PyQt also includes Qt Designer, which allows for anyone to create a GUI much faster using a simple drag and drop graphical interface designer.

Using PyQt’s signal and slot mechanism, you can essentially create your own widgets that can call other Python functions. This will be covered in more detail in Chapter 9.

There are, of course, other toolkits available for creating applications with GUIs using Python, such as Tkinter or wxPython. The many other toolkits have some advantages over PyQt. For example, Tkinter comes bundled with Python, meaning that you can find an abundance of helpful resources by doing a quick search on the Internet.

It is worth noting that if you choose to use PyQt to create commercial applications, you may need to get a license.

Ultimately, it all comes down to choosing the toolkit that works the best for your project.

Requirements

In order to use PyQt, you will first need to have Python 3 installed. To check if Python is already installed on your system or to find out how to download Python, please refer to Appendix B. You will also find a guide to help you refresh your Python skills to aid you while learning PyQt in Appendix B.

Note

As of this writing, Python 2 is set to no longer be maintained. Therefore, all Python code in this book will be written using Python 3. Many of the projects that utilize Python 2 have already started making their way over to Python 3. If you have any questions about the differences between 2 and 3, I suggest checking out http://python-future.org/compatible_idioms.html.

PyQt does not come included with your Python installation. For this book we will be using the PyQt5 toolkit, which is the latest version. Please refer to Appendix A to learn how to download PyQt for your operating system.

Links to Source Code

The source code for Beginning PyQt: A Hands-on Approach to GUI Programming can be found on GitHub via the book’s product page, located at www.​apress.​com/​ISBN.

How This Book Is Organized

In the beginning chapters, we will walk through the code step-by-step, helping to guide you through PyQt classes and concepts for designing GUIs. Chapters 2 and 3 will help you get started using PyQt, adding more and more functionality to your projects. Each chapter teaches how to use different widgets, such as QLabel, QCheckBox, and QLineEdit, and gives examples and ideas of how to use 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, Chapter 5 guides you through examples that help you to create menus and toolbars. Chapter 6 presents style sheets for altering the look of your applications 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 7.

Chapters 8 through 11 begin looking at larger concepts and projects, including using the clipboard to move between applications, graphics, and animation, creating custom widgets, utilizing SQL databases and PyQt’s model/view architecture, and multithreading programming.

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

Appendix A guides you through the process of downloading PyQt5 and includes information about different PyQt classes. Appendix B is there to help you set up Python 3 and to refer back to in case you are not sure about some of the Python code used in this book.

Reader Feedback

Finally, your feedback, questions, and ideas are very important. If you would like to take a moment to let me know your thoughts about the book, you can send comments to the following address:
..................Content has been hidden....................

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