In this article i want to show you How to Create Toolbox in PyQt5. a ToolBox is a widget that displays a column of tabs one above the other, with the current item displayed below the current tab. Every tab has an index position within the column of tabs. A tab’s item is a QWidget.
Also you can read more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
First we need some imports.
1 2 3 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QToolBox, QLabel, QVBoxLayout import sys |
After that we are going to create our Window class that extends from QWidget. we need to initialize some requirements for our window. also we have called our InitWindow() method in this class.
1 2 3 4 5 6 7 8 9 |
class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 ToolBox" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() |
In here we have set the window icon, window title and window geometry.
1 2 3 |
self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) |
If you want to set a color for your window than use this code.
1 |
self.setStyleSheet("background-color:yellow") |
Now we have created QVBoxLayout with QToolBox, and we have added the qtoolbox in the layout.
1 2 3 |
vbox = QVBoxLayout() toolbox = QToolBox() vbox.addWidget(toolbox) |
These are the items that we have added in the QToolbox.
1 2 3 4 5 6 7 |
label = QLabel() toolbox.addItem(label, "Python") toolbox.setFont(QtGui.QFont("Sanserif", 15)) label = QLabel() toolbox.addItem(label, "Java") label = QLabel() toolbox.addItem(label, "C++") |
Also every PyQt5 application must create an application object.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec_()) |
Run the complete code and this will be the result.
Complete source code for How to Create Toolbox in PyQt5
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# Import necessary modules from PyQt5 library from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QToolBox, QLabel, QVBoxLayout import sys # Define main window class class Window(QWidget): def __init__(self): super().__init__() self.title = "Codeloop.org - PyQt5 ToolBox" self.top = 200 self.left = 500 self.width = 400 self.height = 300 # Initialize the window self.InitWindow() def InitWindow(self): # Set window properties self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) # Set background color using stylesheet self.setStyleSheet("background-color:yellow") # Create a vertical layout vbox = QVBoxLayout() # Create a QToolBox widget toolbox = QToolBox() # Add the QToolBox widget to the layout vbox.addWidget(toolbox) # Add items to the QToolBox # Item 1: Python label = QLabel() toolbox.addItem(label, "Python") # Item 2: Java label = QLabel() toolbox.addItem(label, "Java") # Item 3: C++ label = QLabel() toolbox.addItem(label, "C++") # Set font size for QToolBox toolbox.setFont(QtGui.QFont("Sanserif", 15)) # Set layout for window self.setLayout(vbox) # Display the window self.show() # Create PyQt application instance App = QApplication(sys.argv) # Create an instance of Window class window = Window() # Execute application sys.exit(App.exec_()) |
Customizing Toolbox Items in PyQt5
Now let’s customize tool box in PyQt5, and you can use this code for that.
1 |
label1.setStyleSheet("font-size: 20px; color: blue;") |
Now this is the complete code
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# Import necessary modules from PyQt5 library from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QToolBox, QLabel, QVBoxLayout import sys # Define main window class class Window(QWidget): def __init__(self): super().__init__() self.title = "Codeloop - Customizing QToolBox Items" self.top = 200 self.left = 500 self.width = 400 self.height = 300 # Initialize the window self.InitWindow() def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) # Set window properties self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) # Create a vertical layout vbox = QVBoxLayout() # Create a QToolBox widget toolbox = QToolBox() # Add the QToolBox widget to the layout vbox.addWidget(toolbox) # Customize and add items to the QToolBox # Item 1: Python label1 = QLabel("Python") label1.setStyleSheet("font-size: 20px; color: blue;") toolbox.addItem(label1, "Python") # Item 2: Java label2 = QLabel("Java") label2.setStyleSheet("font-size: 18px; color: green;") toolbox.addItem(label2, "Java") # Item 3: C++ label3 = QLabel("C++") label3.setStyleSheet("font-size: 16px; color: red;") toolbox.addItem(label3, "C++") # Set layout for window self.setLayout(vbox) # Display the window self.show() # Create a PyQt application instance App = QApplication(sys.argv) # Create an instance of Window class window = Window() # Execute application sys.exit(App.exec_()) |
In this example, we have created a QToolBox widget and customize the appearance of each item by setting the font size and text color using style sheets. Each item is represented by a QLabel widget containing the text. By adjusting the style sheet properties, you can customize the appearance of toolbox items according to your preferences.
Run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email