In this Python PyQt6 lesson we want to learn How to Create Layouts in Python PyQt6, PyQt6 is powerful and popular Python GUI library for creating graphical user interfaces (GUIs). one of the most important concepts of creating GUI application is creating layout of the elements on the screen. in this article we are going to explore how to create layouts in Python PyQt6.
What is Layout in Python PyQt6 ?
PyQt6 layout a way of arranging widgets such as buttons, labels and text boxes in specific order on the screen. layout can automatically adjust the size and position of widgets based on the size of the window or the contents of other widgets. this makes it easy to create nice and responsive user interfaces.
Types of Layouts in PyQt6
PyQt6 supports several types of layouts including:
- QHBoxLayout: it is horizontal layout that widgets are arranged from left to right
- QVBoxLayout: it is vertical layout that widgets are arranged from top to bottom
- QGridLayout: grid based layout that widgets are arranged in a grid
- QFormLayout: it is layout used for forms that labels and fields are arranged in pairs
How to Create Layouts in Python PyQt6
For creating a layout in Python PyQt6, we need to create container widget such as a QMainWindow or QDialog. after that we can add widgets to the container widget and apply layout to them.
Now let’s create our example, first we need to import our classes and modules from PyQt6.
1 2 |
import sys from PyQt6.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget |
Now let’s create our main window class that extends from QWidget module.
1 2 3 |
class MyWindow(QWidget): def __init__(self): super().__init__() |
In here we have created our QVBoxLayout
1 |
vbox = QVBoxLayout() |
After that we have created our QLabel and added that to the QVBoxLayout, also we have set the main window layout to the QVBoxLayout.
1 2 3 |
self.label = QLabel('Welcome to GeeksCoders.com') vbox.addWidget(self.label) self.setLayout(vbox) |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import sys from PyQt6.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget class MyWindow(QWidget): def __init__(self): super().__init__() vbox = QVBoxLayout() self.label = QLabel('Welcome to GeeksCoders.com') vbox.addWidget(self.label) self.setLayout(vbox) if __name__ == '__main__': app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec()) |
In here we have imported necessary modules like sys and some PyQt6 classes QApplication, QLabel, QVBoxLayout and QWidget.
After that we have defined custom widget called MyWindow, which extends from QWidget. this widget will contain a label with the text Welcome to GeeksCoders.com.
In the constructor __init__ of MyWindow we have created QVBoxLayout which is vertical layout. after that we have created QLabel with the desired text and add it to the layout using addWidget. finally we set the layout of the widget to be the QVBoxLayout we just created.
In the main block we have created an instance of QApplication, an instance of MyWindow, show the window, and start the event loop using app.exec().
Run this code and you will see this output
Learn More on Python GUI
- How to Use Stylesheets in PyQt5
- How to Build Custom Widgets in PyQt6
- How to Create CheckButton in Python TKinter
- Object Tracking with Python & OpenCV
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6