Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Learning the Basics of PyQt

00:00 Learning the Basics of PyQt You’ll need to master the basic components of PyQt if you want to proficiently use this library to develop GUI applications. Some of the components are seen on screen.

00:14 These elements are the building blocks of any PyQt GUI application. Most of them are represented as Python classes that live in the PyQt6 widgets module. These elements are extremely important, so you’ll learn about them next. Widgets are rectangular graphical components that you can place on your application’s windows to build the GUI. Widgets have several attributes and methods that allow you to tweak their appearance and behavior.

00:42 They can also paint a representation of themselves on the screen. Widgets also detect mouse clicks, key presses, and other events from the user, the window system, and other sources.

00:54 Each time a widget catches an event, it emits a signal to announce its state change.

00:59 PyQt has a rich and modern collection of widgets, and each of those serves a different purpose.

01:07 Some of the most common and useful widgets are seen on screen. You’ll see each of these next and the code for the examples that you see on screen are included with the course materials as they may help you get started using them in your own applications.

01:21 You create a button by instantiating QPushButton, a class that provides a classic command button. You can see examples on screen. Buttons like these are perhaps the most commonly used widgets in any GUI.

01:35 When someone clicks them, the app commands the computer to perform an action. This is how you execute computations when a user clicks a button.

01:45 Next up are labels, which you create with QLabel. Labels let you display useful information as text or images. You’ll use labels like these to explain how to use your app’s GUI. You can tweak a label’s appearance in several ways.

02:01 It can accept HTML formatted text as you saw earlier. You can also use labels to specify a keyboard shortcut to move the cursor’s focus to a given widget on your GUI.

02:14 Another common widget is the line edit, also known as an input box. This widget allows you to enter a single line of text. You can create line edits with a QLineEdit class.

02:26 They’re useful when you need to get the user’s input as plain text. Line edits automatically provide basic editing operations, such as copy, paste, undo, redo, drag, drop, and so on.

02:38 On screen, you can see that the objects in the first row show placeholder text to inform the user of the kind of input that’s required.

02:47 Combo boxes are another fundamental type of widget in GUI applications. They’re created by instantiating QComboBox. A combo box will present your user with a dropdown list of options in a way that takes up minimal screen space.

03:02 On screen, you can see an example which provides a dropdown list of popular programming languages. This combo box is read-only, which means that users can select one of several options but can’t add their own.

03:14 Combo boxes can also be editable, allowing users to add new options on the fly. They can also contain PIX Maps, strings, or both.

03:25 The last widget you’ll see here is the radio button, which you can create with QRadioButton. They’re an option button that you can click to switch on.

03:34 Radio buttons are useful when you need the user to select one of multiple options. All options in a radio button are visible on the screen at the same time.

03:46 PyQt has a large collection of widgets. At the time of creating this course, there are over 40 available for you to use to create your application’s GUI. And here you’ve only seen a small sample, but that’s enough to show you the power and flexibility of PyQt.

04:03 In the next section of the course, you’ll learn how to lay out different widgets to build modern and functional GUIs for your applications.

Become a Member to join the conversation.