In this Python PyQt5 article i want to talk about Python PyQt5 QHBoxLayout & QVBoxLayout. also we are going to create example for this .The QHBoxLayout class lines up widgets horizontally. This class is used to construct horizontal box layout objects. The QVBoxLayout class lines up widgets vertically.This class is used to construct vertical box layout objects
Join PyQt5 Full Course for Free
PyQt5 Course
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
What is Python PyQt5 QHBoxLayout ?
PyQt5 QHBoxLayout is class in the PyQt5 library that provides horizontal layout management system for organizing widgets in user interface. it is used to arrange widgets horizontally from left to right in a container widget. QHBoxLayout class is a subclass of the QBoxLayout class, which is generic layout manager that can be used to arrange widgets in both horizontal and vertical directions. however QHBoxLayout is specifically designed for horizontal layouts. to use QHBoxLayout you create an instance of the class and add the widgets you want to include in the layout using the addWidget() method. you can also specify spacing and margins using the setSpacing() and setContentsMargins() methods. once you have added all the widgets, you can set the layout on a container widget using the setLayout() method.
This is an example of PyQt5 QHBoxLayout
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 |
import sys from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton class MyWidget(QWidget): def __init__(self): super().__init__() self.UI() def UI(self): button1 = QPushButton("Codeloop.org") button2 = QPushButton("Codeloop.org") layout = QHBoxLayout() layout.addWidget(button1) layout.addWidget(button2) self.setLayout(layout) self.setGeometry(100, 100, 300, 100) self.setWindowTitle('My Widget') if __name__ == '__main__': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec_()) |
In this example we have created new class MyWidget that inherits from QWidget. after that we have defined an UI() method that sets up the user interface by creating two buttons, creating a QHBoxLayout, adding the buttons to the layout, and setting the layout on the widget. We also set the geometry and window title of the widget.
And lastly we create an instance of MyWidget and show it using the show() method, as well as running the Qt event loop using app.exec_().
Run the code and this will be the result
What is Python PyQt5 QVBoxLayout ?
Python PyQt5 QVBoxLayout is a class in the PyQt5 library that provides vertical layout management system for organizing widgets in a user interface. it is used to arrange widgets vertically from top to bottom in a container widget.
QVBoxLayout class is subclass of the QBoxLayout class which is a generic layout manager that can be used to arrange widgets in both horizontal and vertical directions. however, QVBoxLayout is specifically designed for vertical layouts.
To use QVBoxLayout, you create an instance of the class and add the widgets you want to include in the layout using the addWidget() method. You can also specify spacing and margins using the setSpacing() and setContentsMargins() methods, respectively. Once you have added all the widgets, you can set the layout on a container widget using the setLayout() method.
This is an example of PyQt5 QVBoxLayout
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 |
import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton class MyWidget(QWidget): def __init__(self): super().__init__() self.UI() def UI(self): button1 = QPushButton("Codeloop.org") button2 = QPushButton("Codeloop.org") layout = QVBoxLayout() layout.addWidget(button1) layout.addWidget(button2) self.setLayout(layout) self.setGeometry(100, 100, 100, 200) self.setWindowTitle('My Widget') if __name__ == '__main__': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec_()) |
In this example we have created new class MyWidget that inherits from QWidget. after that we have defined UI() method that sets up the user interface by creating two buttons, creating a QVBoxLayout, adding the buttons to the layout, and setting the layout on the widget. We also set the geometry and window title of the widget.
and lastly we create an instance of MyWidget and show it using the show() method, as well as running the Qt event loop using app.exec_().
Run the complete code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email