#pythontutorial #python #pythonprogramming
PyQt5 Layout Managers
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QLabel, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(700, 300, 500, 500)
self.initUI()
def initUI(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
label1 = QLabel("#1")
label2 = QLabel("#2")
label3 = QLabel("#3")
label4 = QLabel("#4")
label5 = QLabel("#5")
label1.setStyleSheet("background-color: red;")
label2.setStyleSheet("background-color: yellow;")
label3.setStyleSheet("background-color: green;")
label4.setStyleSheet("background-color: blue;")
label5.setStyleSheet("background-color: purple;")
VERTICAL LAYOUT
-----------------
vbox = QVBoxLayout()
vbox.addWidget(label1)
vbox.addWidget(label2)
vbox.addWidget(label3)
vbox.addWidget(label4)
vbox.addWidget(label5)
central_widget.setLayout(vbox)
HORIZONTAL LAYOUT
-----------------
hbox = QHBoxLayout()
hbox.addWidget(label1)
hbox.addWidget(label2)
hbox.addWidget(label3)
hbox.addWidget(label4)
hbox.addWidget(label5)
central_widget.setLayout(hbox)
GRID LAYOUT
-----------------
grid = QGridLayout()
grid.addWidget(label1, 0, 0)
grid.addWidget(label2, 0, 1)
grid.addWidget(label3, 1, 0)
grid.addWidget(label4, 1, 1)
grid.addWidget(label5, 1, 2)
central_widget.setLayout(grid)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
On this page of the site you can watch the video online Layout managers in Python are easy! 🧲 with a duration of hours minute second in good quality, which was uploaded by the user Bro Code 25 July 2024, share the link with friends and acquaintances, this video has already been watched 13,400 times on youtube and it was liked by 221 viewers. Enjoy your viewing!