The QLabel PyQt5 Widget

1. Description of the QLabel PyQt5 class

  1. 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).
  2. 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.
  3. 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.
  4. 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.
  5. 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:

Which displays after execution:

first qlabel pyqt5 - python for beginners

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):

qss style qt qlabel-setStyleSheet qlabel font - python for beginners




4. QLabel PyQt5 according to the object approach

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:

  1. alignment(): Returns the alignment of the text in the QLabel.
  2. autoFillBackground(): Indicates whether the QLabel automatically fills its background.
  3. backgroundRole(): Returns the background role of the QLabel.
  4. buddy(): Returns the companion widget associated with the QLabel.
  5. clear(): Clears the text and pixmap displayed by the QLabel.
  6. font(): Returns the font used by the QLabel.
  7. foregroundRole(): Returns the role of the text color of the QLabel.
  8. frameGeometry(): Returns the geometry of the QLabel, including the border.
  9. frameSize(): Returns the size of the QLabel, including the border.
  10. frameStyle(): Returns the border style of the QLabel.
  11. hasScaledContents(): Whether the pixmap is scaled to fit the size of the QLabel.
  12. hide(): Hides the QLabel.
  13. indent(): Returns the indentation of the text in the QLabel.
  14. isHidden(): Indicates if the QLabel is hidden.
  15. margin(): Returns the margin around the text in the QLabel.
  16. maximumSize(): Returns the maximum size of the QLabel.
  17. minimumSize(): Returns the minimum size of the QLabel.
  18. minimumSizeHint(): Returns the recommended minimum size of the QLabel.
  19. mouseDoubleClickEvent(event): Reimplementation of the mouse double click event handling method for the QLabel.
  20. move(x, y): Moves the QLabel to the specified position.
  21. num(): Returns the text displayed by the QLabel as a number.
  22. palette(): Returns the color palette used by the QLabel.
  23. pixmap(): Returns the pixmap displayed by the QLabel.
  24. resize(width, height): Resizes the QLabel to the specified size.
  25. setAlignment(alignment): Sets the alignment of text in the QLabel.
  26. setAutoFillBackground(enabled): Sets whether the QLabel should fill its background automatically.
  27. setBackgroundRole(role): Sets the background role of the QLabel.
  28. setBuddy(widget): Associates a companion widget to the QLabel.
  29. setFixedHeight(height): Sets the fixed height of the QLabel.
  30. setFixedSize(width, height): Sets the fixed size of the QLabel.
  31. setFixedWidth(width): Sets the fixed width of the QLabel.
  32. setFont(font): Sets the font used by the QLabel.
  33. setForegroundRole(role): Sets the role of the QLabel's text color.
  34. setGeometry(x, y, width, height): Sets the geometry of the QLabel.
  35. setHidden(hidden): Hides or displays the QLabel.
  36. setIndent(indent): Sets the indentation of the text in the QLabel.
  37. setMargin(margin): Sets the margin around the text in the QLabel.
  38. setMaximumHeight(height): Sets the maximum height of the QLabel.
  39. setMaximumSize(width, height): Sets the maximum size of the QLabel.
  40. setMaximumWidth(width): Sets the maximum width of the QLabel.
  41. setMinimumHeight(height): Sets the minimum height of the QLabel.
  42. setMinimumSize(width, height): Sets the minimum size of the QLabel.
  43. setMinimumWidth(width): Sets the minimum width of the QLabel.
  44. setNum(num): Sets the text displayed by the QLabel to be a number.
  45. setPixmap(pixmap): Sets the pixmap displayed by the QLabel.
  46. setScaledContents(enabled): Sets whether the pixmap should be scaled to fit the size of the QLabel.
  47. setStyleSheet(styleSheet): Sets the QLabel's CSS style sheet.
  48. setText(text): Sets the text displayed by the QLabel.
  49. setTextFormat(format): Sets the text format used by the QLabel.
  50. setTextInteractionFlags(flags): Sets the text interaction flags used by the QLabel.
  51. setWordWrap(enabled): Sets whether text should be wrapped automatically in the QLabel.
  52. setWordWrapMode(mode): Sets the text wrapping mode of the QLabel.
  53. show(): Displays the QLabel.
  54. sizeHint(): Returns the recommended size of the QLabel.
  55. text(): Returns the text displayed by the QLabel.
  56. textInteractionFlags(): Returns the text interaction flags used by the QLabel.
  57. textFormat(): Returns the text format used by the QLabel.
  58. update(): Updates the QLabel.
  59. wordWrap(): Indicates whether the text is automatically wrapped in the QLabel.
  60. wordWrapMode(): Returns the text wrapping mode of the QLabel.

 

Younes Derfoufi
CRMEF OUJDA

Leave a Reply