PyQt QGroupBox

The group box provides the top title and shortcut, which can represent a variety of widgets.

The QGroupBox class helps you set the alignment of titles and titles. Group boxes can be checkable, and widgets in the group box become available or impossible, depending on whether the group box is checked or not.

Related course: Create Desktop Apps with Python PyQt5

QGroupBox

To save space, you can enable the flat property, which is usually displayed with no left, right, or bottom edges of the frame.

As shown above, the QGroupBox widget provides a group box frame with a title.

import sys
from PyQt5.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.createFirstExclusiveGroup(), 0, 0)
        grid.addWidget(self.createSecondExclusiveGroup(), 1, 0)
        grid.addWidget(self.createNonExclusiveGroup(), 0, 1)
        grid.addWidget(self.createPushButtonGroup(), 1, 1)

        self.setLayout(grid)
        self.setWindowTitle('Box Layout')
        self.setGeometry(300, 300, 480, 320)
        self.show()

    def createFirstExclusiveGroup(self):
        groupbox = QGroupBox('Exclusive Radio Buttons')

        radio1 = QRadioButton('Radio1')
        radio2 = QRadioButton('Radio2')
        radio3 = QRadioButton('Radio3')
        radio1.setChecked(True)

        vbox = QVBoxLayout()
        vbox.addWidget(radio1)
        vbox.addWidget(radio2)
        vbox.addWidget(radio3)
        groupbox.setLayout(vbox)

        return groupbox

    def createSecondExclusiveGroup(self):
        groupbox = QGroupBox('Exclusive Radio Buttons')
        groupbox.setCheckable(True)
        groupbox.setChecked(False)

        radio1 = QRadioButton('Radio1')
        radio2 = QRadioButton('Radio2')
        radio3 = QRadioButton('Radio3')
        radio1.setChecked(True)
        checkbox = QCheckBox('Independent Checkbox')
        checkbox.setChecked(True)

        vbox = QVBoxLayout()
        vbox.addWidget(radio1)
        vbox.addWidget(radio2)
        vbox.addWidget(radio3)
        vbox.addWidget(checkbox)
        vbox.addStretch(1)
        groupbox.setLayout(vbox)

        return groupbox

    def createNonExclusiveGroup(self):
        groupbox = QGroupBox('Non-Exclusive Checkboxes')
        groupbox.setFlat(True)
        checkbox1 = QCheckBox('Checkbox1')
        checkbox2 = QCheckBox('Checkbox2')
        checkbox2.setChecked(True)
        tristatebox = QCheckBox('Tri-state Button')
        tristatebox.setTristate(True)

        vbox = QVBoxLayout()
        vbox.addWidget(checkbox1)
        vbox.addWidget(checkbox2)
        vbox.addWidget(tristatebox)
        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.addAction('First Item')
        menu.addAction('Second Item')
        menu.addAction('Third Item')
        menu.addAction('Fourth Item')
        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_())

qgroupbox pyqt python

Related course: Create Desktop Apps with Python PyQt5

Description

grid = QGridLayout()
grid.addWidget(self.createFirstExclusiveGroup(), 0, 0)
grid.addWidget(self.createSecondExclusiveGroup(), 1, 0)
grid.addWidget(self.createNonExclusiveGroup(), 0, 1)
grid.addWidget(self.createPushButtonGroup(), 1, 1)

self.setLayout(grid)

Use the grid layout to place the group box.

The groupboxes created by each method are placed at each location through addWidget().

def createFirstExclusiveGroup(self):    
    groupbox = QGroupBox('Exclusive Radio Buttons')

    radio1 = QRadioButton('Radio1')
    radio2 = QRadioButton('Radio2')
    radio3 = QRadioButton('Radio3')
    radio1.setChecked(True)

    vbox = QVBoxLayout()
    vbox.addWidget(radio1)
    vbox.addWidget(radio2)
    vbox.addWidget(radio3)
    groupbox.setLayout(vbox)

return groupbox

The createFirstExclusiveGroup() method creates a groupbox with an exclusive radio button.

The method first creates a groupbox, then a button, and then places it through a vertical box layout.

Finally, set the vertical box layout (vbox) to the layout of the group box.

I created three radio buttons and placed them vertically.

def createSecondExclusiveGroup(self):
    groupbox = QGroupBox('Exclusive Radio Buttons')
    groupbox.setCheckable(True)
    groupbox.setChecked(False)

The createSecondExclusiveGroup() method creates a group box with three radio buttons and one checkbox.

You can also use the setCheckable() method to select a groupbox.

def createNonExclusiveGroup(self):
    groupbox = QGroupBox('Non-Exclusive Checkboxes')
    groupbox.setFlat(True)

The createNonExclusiveGroup() method creates a group box with non-exclusive checkboxes.

Use setFlat() to make the group box look flat.

def createPushButtonGroup(self):
    groupbox = QGroupBox('Push Buttons')
    groupbox.setCheckable(True)
    groupbox.setChecked(True)

The createPushButtonGroup() method creates a group box with multiple pushbuttons.

SetCheckable() and setChecked() to make the group box selectable and selected when executed.