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

Handling Applications, Events and Signals

00:00 Applications, Events, and Signals QApplication is the most foundational class that you’ll use when developing PyQt GUI applications. This class is the core component of any PyQt application.

00:15 It manages the application’s control flow, as well as its main settings. In PyQt, any instance of QApplication is an application. Every PyQt GUI application must have one Q Application instance.

00:30 Some of the responsibilities of the class include handling the app’s initialization and finalization, providing the event loop and event handling, handling most and application-wide settings, providing access to global information such as the applications directory and screen size, passing common command-line arguments, defining the application’s look and feel, and providing localization capabilities.

00:57 These are just some of the core responsibilities of QApplication. So this is a fundamental class when it comes to developing PyQt GUI applications.

01:08 One of the most important responsibilities of QApplication is to provide the event loop and handle events. GUI applications are event-driven.

01:17 This means that functions and methods are called in response to user actions, such as clicking on a button or selecting an item in a combo box. These user actions are commonly known as events and events are handled by an event loop, also known as a main loop.

01:33 An event loop is an infinite loop in which all events from the user, the windows system, and any other sources are processed and dispatched. The event loop waits for an event to occur and then dispatches it to perform a task.

01:46 The event loop continues to work until the application is terminated.

01:52 All GUI applications have an event loop. When an event occurs, the loop checks if it’s a terminated event, If it is, the loop finishes and the application exits.

02:02 Otherwise, the event is sent to the application’s event queue for further processing and the loop iterates once more. You’ve already seen how to run the PyQt event loop by calling exec() on the QApplication object.

02:16 For an event to trigger an action, you need to connect the event with the action that you want to execute. In PyQt, you can establish that connection with the signals and slots mechanism.

02:28 PyQt widgets act as event catches. This means that every widget can catch specific events such as mouse clicks and key presses. In response to these events, a widget emits a signal, which is a message that announces a change in state.

02:43 The signal on its own doesn’t perform any action. If you want a signal to trigger an action, then you need to connect it to a slot. This is the function or method that will perform an action whenever it’s associated signal is emitted.

02:57 You can use any Python callable as a slot.

03:02 If a signal is connected to a slot, then the slot is called whenever the signal is emitted. If it’s not connected to any slot, then nothing happens and the signal is ignored.

03:13 A signal can be connected to one or many slots. A signal may also be connected to another signal, and a slot may be connected to one or many signals.

03:25 You can use the syntax seen on screen to connect a signal and a slot. This will connect slot() function to the widget signal. Whenever signal is emitted, slot() function will be called.

03:40 The code on screen shows you how to use the signals and slots mechanism in a PyQt application.

03:52 A lot of this will look familiar, but there are a couple of new elements. The first is the greet() function, which you’ll use as a slot. It will toggle the presence of the “Hello, World” text.

04:20 In this line, you connect the buttons click signal to Greet. This way, whenever the user clicks the Greet button, the greet() slot is called and the label’s object text will alternate between “Hello, World” and an empty string.

04:48 You can see this program in action on screen. When you click the Greet button, the “Hello, World” message appears and disappears on the application’s main window.

05:06 If the slot() function needs to receive extra arguments, then you can pass them using functools partial. On screen you’ll see how to modify greet() to take an argument.

05:17 First, partial is imported from the functools module.

05:24 Then, greet() is modified to take name as an argument,

05:31 and the text is altered to use an f-string to incorporate the string that’s passed in.

05:43 greet() now needs to receive an argument called name. If you want to connect this updated version of greet() to the click signal, then one way to do it is as seen on screen. The

05:57 the call to partial() returns a function object that behaves similarly to greet() when called with name equals world.

06:05 Now, when the user clicks on the button, the message “Hello, World” will appear in the label just as before. Note that you can use a Lambda function instead of functools.partial, and we’ll leave that as an exercise for you to perform yourself.

06:22 The signals and slots mechanism is what you’ll use to give life to your PyQt GUI applications. This mechanism will allow you to turn user events into actions.

06:33 You can dive deeper into signals and slots by checking out the PyQt documentation on the topic at the link seen on screen. You now know the basics of several important concepts of PyQt.

06:45 With this knowledge and the library’s documentation at hand, you are ready to start developing your own GUI applications. So in the next section, you’ll build your first fully functional GUI application, a calculator.

Become a Member to join the conversation.