from qtpy.QtWidgets import QApplication, QMainWindow, QMenu, QAction class MyWindow(QMainWindow): def __init__(self): super().__init__() menu = QMenu(self) action = QAction('Say hello', self) action.triggered.connect(self.say_hello) menu.addAction(action) self.menuBar().addMenu(menu) def say_hello(self): print('Hello, world!') if __name__ == '__main__': app = QApplication([]) window = MyWindow() window.show() app.exec_()
from qtpy.QtWidgets import QApplication, QMainWindow, QToolBar, QAction class MyWindow(QMainWindow): def __init__(self): super().__init__() toolbar = QToolBar(self) action1 = QAction('Action 1', self) action2 = QAction('Action 2', self) toolbar.addAction(action1) toolbar.addAction(action2) self.addToolBar(toolbar) if __name__ == '__main__': app = QApplication([]) window = MyWindow() window.show() app.exec_()This code creates a QMainWindow window with a toolbar that contains two actions called "Action 1" and "Action 2". When the user clicks on either action, it triggers an event that can be handled by connecting a slot to the action's triggered signal. Overall, these examples demonstrate how QAction can be used to create interactive user interfaces in Python-based applications using the Qt framework.