PyQt5 – QActionGroup
QActionGroup: 在PyQt5应用程序中,许多常见的命令可以通过菜单、工具栏按钮和键盘快捷键来调用,由于用户希望每个命令以相同的方式执行,无论使用何种用户界面,QAction都可以用来表示每个命令的操作。在某些情况下,将QAction对象组合在一起是很有用的,这样用户就可以像单选按钮一样,一次只选择(选中)一个QAction。此外,为了查看动作组的效果,添加到它的动作应该是可检查的。
以下是动作组中的动作在菜单中的样子
语法:
action_group = QActionGroup()
这个action_group是用来添加那些应该位于同一组的QAction的,它们可以在addAction方法的帮助下被添加。下面是一些经常使用的QAction的命令
addAction : 要将QAction添加到其中
setEnabled : 要使QActionGroup启用或停用
setExclusionPolicy : 要对行动组设置排除策略
checkedAction : 它返回当前检查的行动
removeAction : 要从组中删除特定的QAction
actions : 它返回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 menu bar
menubar = self.menuBar()
# creating a selection menu
selMenu = menubar.addMenu('Selection')
# creating QAction Instances
action1 = QAction("One", self)
action2 = QAction("Two", self)
action3 = QAction("Three", self)
action4 = QAction("Four", self)
# making actions checkable
action1.setCheckable(True)
action2.setCheckable(True)
action3.setCheckable(True)
action4.setCheckable(True)
# adding these actions to the selection menu
selMenu.addAction(action1)
selMenu.addAction(action2)
selMenu.addAction(action3)
selMenu.addAction(action4)
# creating a action group
action_group = QActionGroup(self)
# adding these action to the action group
action_group.addAction(action1)
action_group.addAction(action2)
action_group.addAction(action3)
action_group.addAction(action4)
# 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("Action 1 is Checked"))
# adding triggered action to the second action
action2.triggered.connect(lambda: label.setText("Action 2 is Checked"))
# adding triggered action to the third action
action3.triggered.connect(lambda: label.setText("Action 3 is Checked"))
# adding triggered action to the fourth action
action4.triggered.connect(lambda: label.setText("Action 4 is Checked"))
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出:
另一个例子
在这个例子中,我们将创建一个具有多个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("One", self)
action2 = QAction("Two", self)
action3 = QAction("Three", self)
action4 = QAction("Four", self)
# making actions checkable
action1.setCheckable(True)
action2.setCheckable(True)
action3.setCheckable(True)
action4.setCheckable(True)
# adding these actions to the tool bar
toolbar.addAction(action1)
toolbar.addAction(action2)
toolbar.addAction(action3)
toolbar.addAction(action4)
# creating a first action group
action_group1 = QActionGroup(self)
# adding these action to the action group
action_group1.addAction(action1)
action_group1.addAction(action2)
# creating a second action group
action_group2 = QActionGroup(self)
action_group2.addAction(action3)
action_group2.addAction(action4)
# 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("Action 1 is Checked"))
# adding triggered action to the second action
action2.triggered.connect(lambda: label.setText("Action 2 is Checked"))
# adding triggered action to the third action
action3.triggered.connect(lambda: label.setText("Action 3 is Checked"))
# adding triggered action to the fourth action
action4.triggered.connect(lambda: label.setText("Action 4 is Checked"))
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())