
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - Action Event
When using PyQt an action event occurs when a user engages with a graphical user interface component linked to a function like clicking a button choosing a menu option or using a keyboard shortcut. Managing these events includes linking these interactions to the functions or procedures that carry out the intended actions.
Key Components of Action Events in PyQt
- QAction − A QAction signifies an activity that the user can initiate. It may be linked to visual interface components, like menu options, toolbar icons and keyboard shortcuts.
- Signal-Slot Mechanism − PyQt uses a signal-slot mechanism to handle events. Signals are emitted when a particular event occurs, and slots are functions or methods that are called in response to those events.
- Event Handlers − PyQt provides predefined event handlers for common user interactions, such as triggered() for when the action is triggered by the user.
Methods for Handling Action Events
Method | Description |
---|---|
triggered() | Called when the action is triggered by the user. |
hovered() | Called when the mouse cursor hovers over the action. |
toggled() | Called when the action's state changes, typically for toggleable actions like checkboxes. |
Example 1: Simple Button Click Event
In the below example, we'll create a PyQt application with a single button. Clicking the button will trigger an action event that displays a message.
import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QMessageBox class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Button Click Example") button = QPushButton("Click Me!", self) button.clicked.connect(self.display_message) def display_message(self): QMessageBox.information(self, "Message", "Button Clicked!") if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())
Output

Example 2: Menu Item Selection Event
In the below example, we'll create a PyQt application with a menu bar. Selecting a menu item will trigger an action event that displays a message.
import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QMessageBox from PyQt6.QtGui import QAction class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Menu Selection Example") action = QAction("Show Message", self) action.triggered.connect(self.display_message) menu = self.menuBar() file_menu = menu.addMenu("File") file_menu.addAction(action) def display_message(self): QMessageBox.information(self, "Message", "Menu Item Selected!") if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())
Output

Example 3: Toggleable Action Event
In the below example, we'll create a PyQt application with a toggleable action. Clicking the action will toggle its state and display a message accordingly.
import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QMessageBox, QPushButton from PyQt6.QtGui import QAction class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Toggleable Action Example") self.toggle_action = QAction("Toggle", self, checkable=True) self.toggle_action.triggered.connect(self.display_message) self.addAction(self.toggle_action) self.toggle_button = QPushButton("Toggle Action", self) self.toggle_button.clicked.connect(self.toggle_action.trigger) def display_message(self, checked): if checked: QMessageBox.information(self, "Message", "Action Checked!") else: QMessageBox.information(self, "Message", "Action Unchecked!") if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())
Output
