1. Description of the QLabel PyQt5 class
- The QLabel class: is a class of the PyQt5 library which allows to display text, an image or an icon etc. It is often used to display instructions, error messages, images, or icons in a graphical user interface (GUI).
- The QLabel can display text formatted: using the Qt text format. It can also be configured to accept user interactions such as clicks or mouse movements. The QLabel supports defining a style via a CSS style sheet.
- The QLabel can be configured to display an image: using a pixmap. The pixmap can be scaled to fit the size of the QLabel. The QLabel can also be configured to automatically wrap text or to set a minimum and maximum width or height.
- The QLabel class inherits from the QWidget class: and can be used in complex user interfaces. QLabel objects can be added to container layouts such as QVBoxLayout or QHBoxLayout.
- Conclusion: the QLabel class is an important component of PyQt5 that allows text and images to be displayed in PyQt5 user interfaces. It can be used to display messages, instructions, images or icons in GUI applications. The QLabel supports styling through a CSS stylesheet and can be configured to accept user interactions.
2. Creating a PyQt5 QLabel
To create labels within a window, PyQt5 offers us the QLabel class. Just instantiate this class by adding the parent container as a parameter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel app = QApplication(sys.argv) widget = QWidget() widget.setGeometry(50,50,320,200) widget.setWindowTitle("Label Example") # create a QLabel qlabel = QLabel(widget) qlabel.setText("Hello World!") # define the qlabel dimensions & position qlabel.setGeometry(50, 50, 200, 50) widget.show() sys.exit(app.exec_()) |
Which displays after execution:
3. Use a design style for a QLabel
A QLabel object has the setStyleSheet() method which allows you to add a qss style ( Qt Style Sheet):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel app = QApplication(sys.argv) widget = QWidget() widget.setGeometry(50,50,350,200) widget.setWindowTitle("Label Example") # create a QLabel qlabel = QLabel(widget) qlabel.setText("Hello World!") # define the qlabel dimensions & position qlabel.setGeometry(50, 50, 250, 50) # Use QSS designate qlabel.setStyleSheet("background: darkblue; border: 2px solid red; color:yellow; font:broadway; font-size:36px;") widget.show() sys.exit(app.exec_()) |
4. QLabel PyQt5 according to the object approach
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, QLabel class Window(QWidget): def __init__(self , window): super().__init__() self.window = window # create build method def build(self): self.window.setWindowTitle("Example of QLabel by OOP approach") self.window.setGeometry(100 , 100 , 400 , 200) # create a QLabel myLabel = QLabel(self.window) myLabel.setText("Example of QLabel by object approach !") myLabel.setGeometry(50 , 50 , 300 , 50) if __name__ == "__main__": app = QApplication(sys.argv) win = QWidget() myWin = Window(win) myWin.build() win.show() app.exec_() |
5. List of methods associated with a QLabel PyQt5 object
Here is a complete list of methods associated with a QLabel object from the PyQt5 library, sorted alphabetically and with a brief description of each method:
- alignment(): Returns the alignment of the text in the QLabel.
- autoFillBackground(): Indicates whether the QLabel automatically fills its background.
- backgroundRole(): Returns the background role of the QLabel.
- buddy(): Returns the companion widget associated with the QLabel.
- clear(): Clears the text and pixmap displayed by the QLabel.
- font(): Returns the font used by the QLabel.
- foregroundRole(): Returns the role of the text color of the QLabel.
- frameGeometry(): Returns the geometry of the QLabel, including the border.
- frameSize(): Returns the size of the QLabel, including the border.
- frameStyle(): Returns the border style of the QLabel.
- hasScaledContents(): Whether the pixmap is scaled to fit the size of the QLabel.
- hide(): Hides the QLabel.
- indent(): Returns the indentation of the text in the QLabel.
- isHidden(): Indicates if the QLabel is hidden.
- margin(): Returns the margin around the text in the QLabel.
- maximumSize(): Returns the maximum size of the QLabel.
- minimumSize(): Returns the minimum size of the QLabel.
- minimumSizeHint(): Returns the recommended minimum size of the QLabel.
- mouseDoubleClickEvent(event): Reimplementation of the mouse double click event handling method for the QLabel.
- move(x, y): Moves the QLabel to the specified position.
- num(): Returns the text displayed by the QLabel as a number.
- palette(): Returns the color palette used by the QLabel.
- pixmap(): Returns the pixmap displayed by the QLabel.
- resize(width, height): Resizes the QLabel to the specified size.
- setAlignment(alignment): Sets the alignment of text in the QLabel.
- setAutoFillBackground(enabled): Sets whether the QLabel should fill its background automatically.
- setBackgroundRole(role): Sets the background role of the QLabel.
- setBuddy(widget): Associates a companion widget to the QLabel.
- setFixedHeight(height): Sets the fixed height of the QLabel.
- setFixedSize(width, height): Sets the fixed size of the QLabel.
- setFixedWidth(width): Sets the fixed width of the QLabel.
- setFont(font): Sets the font used by the QLabel.
- setForegroundRole(role): Sets the role of the QLabel's text color.
- setGeometry(x, y, width, height): Sets the geometry of the QLabel.
- setHidden(hidden): Hides or displays the QLabel.
- setIndent(indent): Sets the indentation of the text in the QLabel.
- setMargin(margin): Sets the margin around the text in the QLabel.
- setMaximumHeight(height): Sets the maximum height of the QLabel.
- setMaximumSize(width, height): Sets the maximum size of the QLabel.
- setMaximumWidth(width): Sets the maximum width of the QLabel.
- setMinimumHeight(height): Sets the minimum height of the QLabel.
- setMinimumSize(width, height): Sets the minimum size of the QLabel.
- setMinimumWidth(width): Sets the minimum width of the QLabel.
- setNum(num): Sets the text displayed by the QLabel to be a number.
- setPixmap(pixmap): Sets the pixmap displayed by the QLabel.
- setScaledContents(enabled): Sets whether the pixmap should be scaled to fit the size of the QLabel.
- setStyleSheet(styleSheet): Sets the QLabel's CSS style sheet.
- setText(text): Sets the text displayed by the QLabel.
- setTextFormat(format): Sets the text format used by the QLabel.
- setTextInteractionFlags(flags): Sets the text interaction flags used by the QLabel.
- setWordWrap(enabled): Sets whether text should be wrapped automatically in the QLabel.
- setWordWrapMode(mode): Sets the text wrapping mode of the QLabel.
- show(): Displays the QLabel.
- sizeHint(): Returns the recommended size of the QLabel.
- text(): Returns the text displayed by the QLabel.
- textInteractionFlags(): Returns the text interaction flags used by the QLabel.
- textFormat(): Returns the text format used by the QLabel.
- update(): Updates the QLabel.
- wordWrap(): Indicates whether the text is automatically wrapped in the QLabel.
- wordWrapMode(): Returns the text wrapping mode of the QLabel.
Younes Derfoufi
CRMEF OUJDA