In this PyQt6 article we are going to about Event Handling in PyQt6, also we want to talk about Responding to User Actions, Event handling is an important part of any graphical user interface (GUI) application. when user interacts with the application by clicking on a button, entering text or resizing a window, the application needs to respond appropriately. in PyQt6, event handling is achieved through signals and slots, you can install PyQt6 using pip
1 |
pip install PyQt6 |
PyQt6 Signals and Slots
Signal is an event that is emitted by an object when particular action occurs. for example, a button object emits a signal when it is clicked. slot is a function that is called in response to signal. when a signal is emitted, all the connected slots are called in turn.
in PyQt6, you can connect a signal to a slot using the connect() method. the connect() method takes two arguments, the signal to connect and the slot to connect it to.
This is an example of Event Handling in Python PyQt6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import sys from PyQt6.QtWidgets import QApplication, QWidget,QVBoxLayout, QLabel,QPushButton from PyQt6.QtGui import QFont class MyWindow(QWidget): def __init__(self): super().__init__() self.setGeometry(300, 300, 250, 150) self.setWindowTitle('GeeksCoders - Event handling') vbox = QVBoxLayout() button = QPushButton('Click me', self) button.clicked.connect(self.buttonClicked) self.label = QLabel("Hello", self) vbox.addWidget(button) vbox.addWidget(self.label) self.setLayout(vbox) def buttonClicked(self): self.label.setText("Welcome to GeeksCoders.com") self.label.setFont(QFont("Times", 15)) if __name__ == '__main__': app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec()) |
In this example we have created a button object and connect its clicked signal to the buttonClicked() slot function. when button is clicked, the buttonClicked() function is called, and the label text will be changed.
This is the result
PyQt6 Event Types
There are many different types of events that can be handled in PyQt6. some common event types include:
- QEvent.Resize: emitted when the window is resized
- QEvent.Close: emitted when the window is closed
- QEvent.KeyPress: emitted when a key is pressed
- QEvent.MouseButtonPress: emitted when a mouse button is pressed
- QEvent.Wheel: emitted when the mouse wheel is scrolled
You can handle these events by subclassing the appropriate widget class and implementing the corresponding event handler function. for example, to handle key press event, you can subclass the QMainWindow class and implement the keyPressEvent() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import sys from PyQt6.QtWidgets import QApplication, QMainWindow from PyQt6.QtCore import Qt class MyWindow(QMainWindow): def __init__(self): super().__init__() self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Event handling') def keyPressEvent(self, event): if event.key() == Qt.Key.Key_Escape: self.close() if __name__ == '__main__': app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec()) |
In this example, we subclass the QMainWindow class and implement keyPressEvent() function. when the user presses Esc key, the close() method is called to close the window.
Learn More on Python GUI
- How to Create Label in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading