from PyQt5.QtWidgets import QLabel, QWidget, QVBoxLayout # Create a widget widget = QWidget() # Create a QLabel with "Hello World" text label = QLabel("Hello World") # Set the size policy of the label to Fixed label.setSizePolicy(QLabel.Fixed, QLabel.Fixed) # Add the label to a layout layout = QVBoxLayout(widget) layout.addWidget(label) # Display the widget widget.show()
from PyQt5.QtCore import Qt from PyQt5.QtGui import QImage from PyQt5.QtWidgets import QLabel, QWidget, QHBoxLayout # Create a widget widget = QWidget() # Load an image from file image = QImage("example.png") # Create a QLabel with the loaded image label = QLabel() label.setPixmap(QPixmap.fromImage(image)) # Set the size policy of the label to Expanding label.setSizePolicy(QLabel.Expanding, QLabel.Expanding) # Add the label to a layout layout = QHBoxLayout(widget) layout.setAlignment(Qt.AlignCenter) layout.addWidget(label) # Display the widget widget.show()In this example, a QLabel with an image loaded from file is created and added to a QHBoxLayout. The size policy of the label is set to Expanding using setSizePolicy() to allow it to fill the available space. The image is also displayed centered in the layout using setAlignment(). The PyQt5.QtGui package is used to load and display images.