How to Create Label in PyQt6 – QLabel

In this PyQt6 lesson we want to learn How to Create Label in PyQt6, for creating Label we want to use QLabel class from PyQt6.

 

 

What is QLabel in PyQt6?

QLabel is a widget in PyQt6, and it is designed to display text or images. It supports rich text formatting, alignment options, word wrapping and pixmap images.

 

How to Create Simple Label in PyQt6?

Let’s start by creating a basic PyQt6 application with a QLabel. This is a simple example showing how to create a window with QLabel displaying text:

In this example, we have imported the necessary PyQt6 modules and define a MainWindow class inheriting from QMainWindow. Inside the MainWindow constructor, we have created a QLabel instance, and set it as the central widget of the main window.

 

 

Run the code and this will be the result

How to Create Label in PyQt6 - QLabel
How to Create Label in PyQt6 – QLabel

 

 

 

Customizing QLabel:

QLabel offers different properties and methods for customizing its appearance and behavior. For example, you can set the text alignment, adjust the font size and style, apply word wrapping and set tooltips. This is an example for the customization of PyQt6 label.

In the above code, we have created a QLabel instance, set its text, align it to the center, apply a bold Arial font with a font size of 12, also enabled word wrapping, and set a tooltip for additional information.

 

 

Displaying Images with PyQt6 QLabel:

Besides text, QLabel can also display images, and it provides best visualization for graphical content. You can load images from files or QPixmap objects and set them as the label’s pixmap. This is an example showing how to display an image with QLabel:

This Python script demonstrates how to display an image using PyQt6.

  1. We create a subclass of QMainWindow called MainWindow.
  2. Inside MainWindow, we have initialized the main window with a title and created a QLabel widget to display the image.
  3. The load_image method is responsible for loading the image file and displaying it in the QLabel.
  4. When the script is executed, an instance of MainWindow is created, and the image is loaded and displayed within the main window.

 

 

 

Run the code and this will be the result

How to Create Label in PyQt6
How to Create Label in PyQt6

 

 

FAQs

 

Q: How can I display an image in PyQt6?

A: You can display an image in PyQt6 by using QPixmap class to load the image file, and after that you can set it to a QLabel widget. The QLabel widget will serve as the container for displaying the image within your PyQt6 application.

 

 

Q: What types of image formats are supported in PyQt6?

A: PyQt6 supports different image formats, including PNG, JPEG, GIF, BMP, and many others. You can load images in any of these formats using the QPixmap class.

 

 

Q: How do I resize an image in PyQt6?

A: To resize an image in PyQt6, you can set the scaledContents property of the QLabel widget to True. This property ensures that the image is automatically scaled to fit the size of the QLabel, maintaining its aspect ratio.

 

 

Q: Is it possible to display multiple images in PyQt6?

A: Yes, you can display multiple images in PyQt6 by creating multiple QLabel widgets and setting each QLabel to a different QPixmap object representing an image. After that you can arrange these QLabel widgets as needed within your PyQt6 application.

 

 

Learn More on PyQt6:

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Codeloop
Share via
Copy link
Powered by Social Snap
×