QActionGroup: en las aplicaciones PyQt5, se pueden invocar muchos comandos comunes a través de menús, botones de la barra de herramientas y atajos de teclado, ya que el usuario espera que cada comando se realice de la misma manera, independientemente de la interfaz de usuario utilizada, QAction es útil para representar cada comando como una acción. En algunas situaciones, es útil agrupar objetos QAction, de modo que el usuario solo pueda seleccionar (marcar) solo un QAction a la vez, como si fueran botones de radio. Además, para ver el efecto del grupo de acciones, la acción agregada debe poder verificarse.
A continuación se muestra cómo se verá un grupo de acciones en acción dentro del menú
Sintaxis:
action_group = QActionGroup()
Este action_group se usa agregando aquellos QAction que deberían estar en el mismo grupo, se pueden agregar con la ayuda del método addAction. A continuación se muestran algunos comandos de uso frecuente con QAction
addAction : To add QAction to it setEnabled : To make QActionGroup enable or disable setExclusionPolicy : To set exclusion policy to the action group checkedAction : It returns the currently checked action removeAction : To remove the specific QAction from the group actions : It returns the list of QAction group is having
Ejemplo:
en esto, crearemos una barra de menú que tendrá un menú y tendrá múltiples QAction verificables, a continuación se muestra la implementación
Python3
# 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())
Producción :
Otro ejemplo
En esto, crearemos una barra de herramientas que tenga múltiples QAction que se dividen en dos grupos de acción, a continuación se muestra la implementación.
Python3
# 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())
Producción :
Publicación traducida automáticamente
Artículo escrito por rakshitarora y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA