Creating Your First App
00:00 Creating Your First PyQt Application Now that you have a working PyQt installation, you are ready to create your first GUI app. You’ll create a Hello World application with Python and PyQt.
00:15
Here are the steps that you’ll follow: Import QApplication
and all the required widgets from PyQt6.QtWidgets, create an instance of QApplication
, create the application’s GUI, show the application’s GUI, and run the application’s event loop or main loop.
00:35
To kick things off, start by creating a new file called hello.py
in your current working directory. First, you import sys
, which will allow you to handle the application’s termination and exit status through the exit()
function.
00:51
Next, you import QApplication
, QLabel
, and QWidget
from QtWidgets
, which is part of the PyQt6
package.
00:58
You’ve now imported everything you’ll need. Next, you create an instance of QApplication
. Do this as you would create an instance of any Python class.
01:12
You should create your app instance before you create any GUI object in PyQt. Internally, the QApplication
class deals with command line arguments.
01:21 That’s why you need to pass in a list of command-line arguments to the class constructor. In this example, you use an empty list because your app won’t be handling any command line arguments.
01:34
You’ll often find that developers pass sys.argv
to the constructor of QApplication
. This object contains the list of command-line arguments passed into a Python script.
01:45 If your application needs to accept command-line arguments, then you should use this to handle them, but otherwise, just use an empty list as you’ve seen.
01:56
Next, you’ll create the application’s GUI. Here, the GUI will be based on the QWidget
class, which is the base class of all user interface objects in PyQt.
02:07
In this code, window
is an instance of QWidget
, which provides all the features that you’ll need to create the application’s window or form. As the name suggests, setWindowTitle()
sets the window title in your application.
02:22 Here the window will show “PyQt App” as its title.
02:28
You can use setGeometry()
to define the window size and screen position. The first two arguments are the X and Y screen coordinates where the window will be placed starting from the top left of the screen.
02:39
The third and fourth arguments are the window’s width and height. Every GUI application needs widgets or graphical components. In this example, you’ll use a QLabel
widget.
02:52
Hello, message
to show the message “Hello world” on your application’s window. QLabel
objects can display HTML formatted text. So here the text is formatted as an H1 header to make it larger on screen.
03:08
Finally, you use move()
to place Hello, message
at the coordinates 60, 15 on the application’s window.
03:17
In PyQt, you can use any widget, a subclass of QWidget
as a top-level window. The only condition is that the target widget must not have a parent widget.
03:28 When you use a widget as your top-level window, PyQt automatically provides it with a title bar and turns it into a normal window.
03:37 The parent-child relationship between widgets has two complementary purposes. A widget with no parent is considered a main or top-level window. In contrast, a widget with an explicit parent is a child widget and is shown within its parent.
03:53 This relationship is also known as ownership with parents owning their children. The PyQt ownership model ensures that if you delete a parent widget such as the top-level window, then all of the child widgets will automatically be deleted as well.
04:08
To avoid memory leaks, you should always make sure that any QWidget
object has a parent with the sole exception of the top-level windows.
04:18 Now, you can continue with the code and get the PyQt GUI application ready to run.
04:26
You call show()
on window. This schedules a paint event, which is a request to paint the widgets that compose a GUI. This event is then added to the application’s event queue.
04:37 You’ll learn more about PyQt’s event loop later on in the course.
04:43
Finally, you start the application’s event loop by calling exec()
. The call to exec()
is wrapped in a call to sys.exit()
, which allows you to cleanly exit Python and release memory resources when the application terminates.
04:59 You can run your first PyQt app as you would any other Python script.
05:05
You should see a window that will look similar to the one on screen depending on your operating system and appearance settings. The application shows a window based on QWidget
.
05:15
The window displays the “Hello world” message, and to show the message, it uses a QLabel
widget. And with that, you’ve written your first GUI desktop application using PyQt and Python.
05:27 You can close it in the normal way with a close button for the window on your operating system. So now that you’ve written your first GUI application, in the next section you’ll look at the basics of Qt so you can understand how to create fully featured applications.
Become a Member to join the conversation.