📜  PyQt5 – QAction

📅  最后修改于: 2022-05-13 01:55:29.071000             🧑  作者: Mango

PyQt5 – QAction

QAction :在 PyQt5 应用程序中,许多常用命令可以通过菜单、工具栏按钮和键盘快捷键调用,因为用户希望每个命令都以相同的方式执行,而不管使用什么用户界面,QAction 可用于将每个命令表示为一种行为。操作可以添加到菜单和工具栏,并自动保持同步。例如,在文字处理器中,如果用户按下粗体工具栏按钮,将自动选中粗体菜单项。

以下是操作在工具栏中的外观

句法:

action = QAction(name)

可以借助addActionaddActions方法将此操作添加到工具栏或 QMenus。以下是 QAction 的一些常用命令

setCheckable : To make QAction Chekable

setIcon : To add icon to the QAction

setText : To set the display name of the QAction

text : To get the display name of the QAction

setPriority : To set the priority of the action

triggered.connect : To connect an method with it when triggered signal is emitted

例子 :
在这个例子中,我们将创建一个带有工具栏的主窗口,标签和工具栏由 QAction 组成,每个都有单独的方法连接到它,下面是实现

# importing libraries
from PyQt5.QtWidgets import * from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * from PyQt5.QtCore import * import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 500, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
  
  
    # method for components
    def UiComponents(self):
  
        # creating a tool bar
        toolbar = QToolBar(self)
  
        # setting geometry to the tool bar
        toolbar.setGeometry(50, 100, 300, 35)
  
  
        # creating QAction Instances
        action1 = QAction("First Action", self)
        action2 = QAction("Second Action", self)
        action3 = QAction("Third Action", self)
  
        # adding these actions to the tool bar
        toolbar.addAction(action1)
        toolbar.addAction(action2)
        toolbar.addAction(action3)
  
        # creating a label
        label = QLabel("GeeksforGeeks", self)
  
        # setting geometry to the label
        label.setGeometry(100, 150, 200, 50)
  
        # adding triggered action to the first action
        action1.triggered.connect(lambda: label.setText("First Action Triggered"))
  
        # adding triggered action to the second action
        action2.triggered.connect(lambda: label.setText("Second Action Triggered"))
  
        # adding triggered action to the third action
        action3.triggered.connect(lambda: label.setText("Third Action Triggered"))
  
  
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :

另一个例子
在本例中,我们将创建一个 commandlink 按钮并向其添加具有 QAction 的菜单,下面是实现

# importing libraries
from PyQt5.QtWidgets import * from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * from PyQt5.QtCore import * import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title 
        self.setWindowTitle("Python ")
  
        # setting geometry 
        self.setGeometry(100, 100, 500, 400)
  
        # calling method 
        self.UiComponents()
  
        # showing all the widgets 
        self.show()
  
        # method for components
  
    def UiComponents(self):
        # creating a command link button
        cl_button = QCommandLinkButton("Press", self)
  
        # setting geometry 
        cl_button.setGeometry(150, 100, 150, 60)
  
        # QActions 
        action1 = QAction("Geeks", self)
        action2 = QAction("GfG", self)
  
        # making action2 checkable
        action2.setCheckable(True)
  
  
  
        # QMenu 
        menu = QMenu()
  
        # adding actions to menu 
        menu.addActions([action1, action2])
  
        # setting menu to the button 
        cl_button.setMenu(menu)
  
  
        # creating label 
        label = QLabel("GeeksforGeeks", self)
  
        # setting label geometry 
        label.setGeometry(100, 200, 300, 80)
  
        # making label multiline 
        label.setWordWrap(True)
  
        # adding method to the action
        action1.triggered.connect(lambda: label.setText("Action1 is triggered"))
  
        # adding method to the action2 when it get checked
        action2.toggled.connect(lambda: label.setText("Action2 is toggled"))
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window 
window = Window()
  
# start the app 
sys.exit(App.exec()) 

输出 :