A QGroupBox draws a frame with a title around a set of widgets. It does not change any behaviour, it just makes a form easier to read once it has more than a few inputs.

What is a GroupBox in PyQt5?
At its core, a GroupBox serves as a container for other widgets. Think of it as a frame where you can place buttons, texts, images, and other UI elements. This is especially useful in complex applications where you need to present multiple options or categories to the user.
The hierarchy works as follows:
- A main window in PyQt5 can contain a layout such as a grid.
- Within this grid layout, one can add multiple group boxes.
- Inside these group boxes, you can place the desired widgets.
For the grid structure, the class QGridLayout is used. As with other layouts, it’s essential to add this to your main application window. On the other hand, QGroupBox is the class responsible for creating a groupbox.
Step-by-Step GroupBox Implementation
The following code provides a concise example of implementing a 2x2 grid of group boxes within a window. Each groupbox contains radio buttons with different food items. Check it out:
# Source: https://pythonprogramminglanguage.com/pyqt5-groupbox/ |
to make the most out of PyQt5 in your projects.
How to use one
- Create it with
groupbox = QtWidgets.QGroupBox('Settings'). The string is the title. - Put the widgets in a layout inside the groupbox.
- Add that layout to the groupbox with
groupbox.setLayout(layout). - Add the groupbox itself to the window layout.
setFlat(True)drops the border,setTitle('...')changes the text.setCheckable(True)adds a tick box that greys out everything inside.setEnabled(False)disables the whole group at once.
Nesting groupboxes works too. One for the general settings and one inside it for the advanced ones.
The fastest way to learn this is to write it yourself. The PyChallenge exercises run in your browser, no install needed.
