Toolbox (QToolBox) is a container widget in PyQt. The widget can show groups of items separated by tabs. If there are to many items for a toolbar, you may want a toolbox.

A screenshot of a toolbox QToolBox is shown below.

toolbox pyqt qtoolbox

Related Course: Create GUI Apps with Python PyQt5

Toolbox example

QToolBox

A QToolBox widget shows a column of tabs one above the other. The current item is shown below the current tab. Every tab has an index position and every tab’s item is a QWidget. A toolbox (QToolBox) can be created with a single line of code:

1
toolbox = QToolBox()

After creationg you can add items to the toolbox with the method addItem(). For example:

1
toolbox.addItem(label, "Students")

The Python code below creates a toolbox with 3 items. The toolbox QToolBox has a method .addItem(), which is used to add it ems.

The toolbox itself has to be added to a layout, for instance layout.addWidget(toolbox, 0, 0).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):

def __init__(self):
QWidget.__init__(self)
layout = QGridLayout()
self.setLayout(layout)

# Add toolbar and items
toolbox = QToolBox()
layout.addWidget(toolbox, 0, 0)
label = QLabel()
toolbox.addItem(label, "Students")
label = QLabel()
toolbox.addItem(label, "Teachers")
label = QLabel()
toolbox.addItem(label, "Directors")

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

Download Examples

Methods

The QToolBox has many methods that can be used, you’ve seen .addItem() before but there are many more.

  • addItem()
  • count()
  • currentIndex()
  • insertItem()
  • itemToolTip()
  • itemText()
  • itemIcon()
  • isItemEnabled()
  • removeItem()
  • setItemEnabled()
  • setItemIcon()
  • setItemText()
  • setItemToolTip()

The example below demonstrates the use of some of these methods:

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
37
38
39
40
41
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):

def __init__(self):
QWidget.__init__(self)
layout = QGridLayout()
self.setLayout(layout)

# Add toolbar and items
toolbox = QToolBox()
layout.addWidget(toolbox, 0, 0)
label = QLabel()
toolbox.addItem(label, "Students")
label = QLabel()
toolbox.addItem(label, "Teachers")
label = QLabel()
toolbox.addItem(label, "Directors")

# show number of items
print(toolbox.count())

# disable tab
toolbox.setItemEnabled(0, False)

# mouseover tooltip
toolbox.setItemToolTip(0, "This is a tooltip")

# tests if items are enabled
print(toolbox.isItemEnabled(0))
print(toolbox.isItemEnabled(1))

# insert item
item = QLabel()
toolbox.insertItem(1, item, "Python")

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

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

Download Examples