You can place widgets inside a grid. A grid can contain groups, where each group has one or more widgets.

groupbox in python pyqt

Related course:


A groupbox and grid can be created with PyQt. It works like this:

  1. A PyQt5 window can contain a grid.
  2. A grid can contain any number of groups
  3. groups can contain widgets (buttons, texts, images).

A grid can be created with the class QGridLayout. As you’d expect, the grid layout has to be added to the window. A groupbox is created with the class QGroupBox.

Groupbox Example

The code below creates a 2x2 groupbox in a window. We create a groupbox using QGridLayout. This in turn can have widgets. Widgets are added using the method addWidget(widget, x,y).

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QCheckBox, QGridLayout, QGroupBox,
QMenu, QPushButton, QRadioButton, QVBoxLayout, QWidget)

class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)

grid = QGridLayout()
grid.addWidget(self.createExampleGroup(), 0, 0)
grid.addWidget(self.createExampleGroup(), 1, 0)
grid.addWidget(self.createExampleGroup(), 0, 1)
grid.addWidget(self.createExampleGroup(), 1, 1)

self.setWindowTitle("PyQt5 Group Box")
self.resize(400, 300)

def createExampleGroup(self):
groupBox = QGroupBox("Best Food")

radio1 = QRadioButton("&Radio pizza")
radio2 = QRadioButton("R&adio taco")
radio3 = QRadioButton("Ra&dio burrito")


vbox = QVBoxLayout()

return groupBox

if __name__ == '__main__':
app = QApplication(sys.argv)
clock = Window()