PyQt - QGroupBox



A group box is a collection of rectangular frames which controls the labels. These boxes help us to achieve a structured screen design.

The QGroupBox is another widget of group that provides a box frame with a title. It is often used to visually group and organize other widgets within it. The purpose of QGroupBox is to create a container with a title that helps in categorizing and presenting related widgets in a structured manner.

In Layout Management, QGroupBox does not perform any action instead it uses two different layouts- QVBoxLayout or QHBoxLayout. Using these layouts create a group box to arrange the widgets.

In addition to this, we have methods like QPushButton() and addWidgets() that interlink to create a clickable button. It is helpful when the user clicks on any single button.

Example 1

Following example demonstrate the code snippet of QGroupBox using PyQt.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QGroupBox, QPushButton
class MyWidget(QWidget):
   def __init__(self):
      super().__init__()
      self.init_ui()
   def init_ui(self):
      self.setWindowTitle('QGroupBox')
      self.setGeometry(300, 300, 300, 200)

      # Create a QVBoxLayout for the main layout
      main_layout = QVBoxLayout(self)

      # Create a QGroupBox
      group_box = QGroupBox('Select your major area of interest-')

      # Create a layout for the group box
      group_layout = QVBoxLayout(group_box)

      # Add widgets to the group box
      button1 = QPushButton('Python')
      button2 = QPushButton('Machine Learning')
      button3 = QPushButton('Deep Learning')
      button4 = QPushButton('Artificial Intelligence')

      group_layout.addWidget(button1)
      group_layout.addWidget(button2)
      group_layout.addWidget(button3)
      group_layout.addWidget(button4)

      # Set the layout of the group box
      group_box.setLayout(group_layout)

      # Add the group box to the main layout
      main_layout.addWidget(group_box)

if __name__ == '__main__':
   app = QApplication(sys.argv)
   widget = MyWidget()
   widget.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

QGroupBox example one

Example 2

Following example demonstrates the set of multiple widgets such as checkboxes, radio buttons, push buttons and enables an option using PyQt.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QGroupBox, QRadioButton, QCheckBox, 
QPushButton, QMenu, QGridLayout, QVBoxLayout
class MyApp(QWidget):
   def __init__(self):
      super().__init__()
      self.initUI()
   def initUI(self):
      grid = QGridLayout()
      grid.addWidget(self.createGroupBox('Radio Buttons', ['Radio1', 'Radio2',  'Radio3']), 0, 0)
      grid.addWidget(self.createGroupBox('Check Boxes', ['Radio1', 'Radio2',  'Radio3'], include_checkbox=True), 1, 0)
      grid.addWidget(self.createPushButtonGroup(), 1, 1)
      self.setLayout(grid)
      self.setWindowTitle('QGroupBox')
      self.setGeometry(300, 300, 480, 320)
      self.show()
   def createGroupBox(self, title, items, include_checkbox=False, is_flat=False,  is_tristate=False):
      groupbox = QGroupBox(title)
      groupbox.setFlat(is_flat)
      vbox = QVBoxLayout()
      for item_text in items:
         if include_checkbox:
            widget = QCheckBox(item_text)
            widget.setTristate(is_tristate)
         else:
            widget = QRadioButton(item_text)

      vbox.addWidget(widget)
      vbox.addStretch(1)
      groupbox.setLayout(vbox)
      return groupbox
   def createPushButtonGroup(self):
      groupbox = QGroupBox('Push Buttons')
      groupbox.setCheckable(True)
      groupbox.setChecked(True)

      pushbutton = QPushButton('Normal Button')
      togglebutton = QPushButton('Toggle Button')
      togglebutton.setCheckable(True)
      togglebutton.setChecked(True)
      flatbutton = QPushButton('Flat Button')
      flatbutton.setFlat(True)
      popupbutton = QPushButton('Popup Button')
      menu = QMenu(self)
      menu.addActions([menu.addAction(f'Item {i}') for i in range(1, 5)])
      popupbutton.setMenu(menu)
      vbox = QVBoxLayout()
      vbox.addWidget(pushbutton)
      vbox.addWidget(togglebutton)
      vbox.addWidget(flatbutton)
      vbox.addWidget(popupbutton)
      vbox.addStretch(1)
      groupbox.setLayout(vbox)
      return groupbox

if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = MyApp()
   sys.exit(app.exec())

Output

The above code produces the following output −

QButtonBox multiple box
Advertisements