A groupbox QGroupBox can group widgets, it provides a frame, title on top and it can display a multiple of widgets inside. It typically has a title and a border. Any PyQt widget can be added into the group box. This can be further used to communciate your UI/UX to your user.

This example demonstrates how to create the groupbox as shown below:

pyqt groupbox

Related Course: Create GUI Apps with Python PyQt5

QGroupBox

PyQt Groupbox

The groupbox is initialized with QGroupBox("title"). Using the constructor is the normal way to set the title (you can also set the alignment: top, bottom, left, right, center). A layout is then added to the groupbox. Widgets are added to the layout.

A QGroupBox doesn’t layout the widgets automatically, so you have to do that yourself. You can use QVBoxLayout or QHBoxLayout for that.

A groupbox can be checkable. All of that said, that gives us the following initializiation:

1
2
3
4
5
6
groupbox = QGroupBox("GroupBox Example")
groupbox.setCheckable(True)
layout.addWidget(groupbox)

vbox = QVBoxLayout()
groupbox.setLayout(vbox)

Individual widgets can then be added to the QVBoxLayout.

1
2
3
4
vbox.addWidget(radiobutton)
vbox.addWidget(radiobutton)
vbox.addWidget(radiobutton)
...

This example below creates a checkable groupbox, title and with several widgets add to it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from PyQt5.QtWidgets import *
import sys

class GroupBox(QWidget):

def __init__(self):
QWidget.__init__(self)

self.setWindowTitle("GroupBox")
layout = QGridLayout()
self.setLayout(layout)

groupbox = QGroupBox("GroupBox Example")
groupbox.setCheckable(True)
layout.addWidget(groupbox)

vbox = QVBoxLayout()
groupbox.setLayout(vbox)

radiobutton = QRadioButton("RadioButton 1")
vbox.addWidget(radiobutton)

radiobutton = QRadioButton("RadioButton 2")
vbox.addWidget(radiobutton)

radiobutton = QRadioButton("RadioButton 3")
vbox.addWidget(radiobutton)

radiobutton = QRadioButton("RadioButton 4")
vbox.addWidget(radiobutton)


app = QApplication(sys.argv)
screen = GroupBox()
screen.show()
sys.exit(app.exec_())

If you are new to Python PyQt, then I highly recommend this book.

Download Examples