Python PyQt5 LAYOUT MANAGERS are easy! 🧲

Published: 25 July 2024
on channel: Bro Code
4,532
96

#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()


Watch video Python PyQt5 LAYOUT MANAGERS are easy! 🧲 online without registration, duration hours minute second in high quality. This video was added by user Bro Code 25 July 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 4,532 once and liked it 96 people.