PyQt5 – QAction

PyQt5 – QAction

QAction: 在PyQt5应用程序中,许多常见的命令可以通过菜单、工具栏按钮和键盘快捷键来调用,由于用户希望每个命令以相同的方式执行,无论使用何种用户界面,QAction对于将每个命令表示为一个动作非常有用。动作可以被添加到菜单和工具条上,并会自动保持它们的同步性。例如,在一个文字处理程序中,如果用户按了粗体工具栏的按钮,粗体菜单项就会自动被选中。下面是一个动作在工具条内的样子

PyQt5 - QAction

语法。

action = QAction(name)

这个动作可以在addAction和addActions方法的帮助下被添加到工具栏或QMenus。下面是一些经常使用的QAction的命令

setCheckable : 要使QAction可检查

setIcon : 为QAction添加图标

setText : 要设置QAction的显示名称

text : 要获得QAction的显示名称

setPriority : 要设置QAction的优先级

triggered.connect : 当触发信号发出时,要将一个方法与之相连

例子: 在这个例子中,我们将创建一个主窗口,它有一个工具条,标签和工具条是由QAction组成的,每个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())

另一个例子 在这个例子中,我们将创建一个命令链接按钮,并为其添加菜单,其中有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())

输出:

PyQt5 - QAction

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程