I know many of you are just looking for some reference code to copy so I put that here at the top, but if you want to learn more, a detailed explanation can be found in the video above and in the text below.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtGui import QFont, QFontDatabase, QPixmap
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QLabel Window")
self.label = QLabel("This is a label.")
self.label.setText("This is an updated label.")
# fonts = QFontDatabase().families()
# print(fonts)
self.label.setFont(QFont('Papyrus', 40))
# Bonus: Images in QLabels
pixmap = QPixmap('cat.png')
self.label.setPixmap(pixmap)
# Attribution:
# Cat image created by Freepik
# - Flaticon: https://www.flaticon.com/free-icons/cat
self.setCentralWidget(self.label)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
cat.png
to download:

Boilerplate Code
For us to work with QLabels, we will need to setup a window to quickly do that. The code for which can be found below:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QLabel Window")
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
Starting off at the top, we have some imports for the things we will need.
Lines 4-7, subclass QMainWindow
in order to allow us to create our own custom main window to add content to. Then it initializes the class by running the super class’ init()
method, and setting the title for the window to "My QLabel Window"
.
Lines 10-14 define the main()
function which creates an application initialized with the given command line arguments (sys.argv
), creates a main window from our custom class, shows the window, and executes the application.
Lastly, lines 17 and 18 runs the main()
function if this is the main file being run.
If you save the file and run it, it should look like this:

Now onto QLabels
!
1. Creating and Initializing QLabels
Let’s add a QLabel to the window!
First you will need to update line 2 to import QLabel
:
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
Then on line 8, create and initialize a QLabel
as seen below:
self.label = QLabel("This is a label.")
We are passing in "This is a label."
which sets the text of the label to that string.
Then to add the label to the window we use QMainWindow
‘s setCentralWidget
function passing it in self.label
as seen below:
self.setCentralWidget(self.label)
Click here for the full code example…
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QLabel Window")
self.label = QLabel("This is a label.")
self.setCentralWidget(self.label)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
And if we run that, we should see this window:

Congratz! You have created and initialized a QLabel
.
2. Updating a QLabel
Updating QLabels after creation or due to user interactions is very common. So let’s see how to update a label.
It is as easy as using QLabel
‘s setText()
method and passing it the text you want to update the label with. As shown below, I just add this on line 10 of our example passing in "This is an updated label."
:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QLabel Window")
self.label = QLabel("This is a label.")
self.label.setText("This is an updated label.")
self.setCentralWidget(self.label)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
If we run it we should get something like below:

Now let’s get to styling!
3. Updating a QLabel
One of the next most common things is to style a QLabel. I am going to show you one of the easiest ways to set the font and size of the text.
First, we should probably find out what fonts are available to PyQt. First, you will need to import QFontDatabase:
from PyQt5.QtGui import QFontDatabase
The all you need to do is add these two lines of code:
fonts = QFontDatabase().families()
print(fonts)
I added them after I updated the text of the label starting on line 13. This gets a list of the fonts available, saving it into fonts
, and then prints it out.
From that you should be able to pick a font to use. I picked Papyrus for this example. To change the font, you will need to update line 2 to import QFont
:
from PyQt5.QtGui import QFont, QFontDatabase
Then you will use QLabel’s setFont method to set a font, passing it in the font we want to set. In this case I pass it in QFont('Papyrus', 40)
. This creates a QFont
set to Papyrus
with a size of 40
:
self.label.setFont(QFont('Papyrus', 40))
Click here for the full code example…
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtGui import QFont, QFontDatabase
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QLabel Window")
self.label = QLabel("This is a label.")
self.label.setText("This is an updated label.")
# fonts = QFontDatabase().families()
# print(fonts)
self.label.setFont(QFont('Papyrus', 40))
self.setCentralWidget(self.label)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
And if you run that, you should get something like below:

There are a few ways to go about styling a QLabel
. If you want to see how to do more than that, feel free to drop us a message!
BONUS: Images in QLabels
It turns out that QLabels aren’t just for text. They can have images in them too! So let’s see how we can add an image to a QLabel.
First, we will need to add an import to QPixmap
on line 3:
from PyQt5.QtGui import QFontDatabase, QFont, QPixmap
Then starting on line 17, add the following code:
pixmap = QPixmap('cat.png')
self.label.setPixmap(pixmap)
This loads in the image 'cat.png'
from the directory the code was run from, and on the next line sets the label to use the image.
The image I use in this example is below. Save it as cat.png wherever you are running your code from.

Click here for the full code example…
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtGui import QFont, QFontDatabase, QPixmap
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QLabel Window")
self.label = QLabel("This is a label.")
self.label.setText("This is an updated label.")
# fonts = QFontDatabase().families()
# print(fonts)
self.label.setFont(QFont('Papyrus', 40))
pixmap = QPixmap('cat.png')
self.label.setPixmap(pixmap)
# Attribution:
# Cat image created by Freepik - Flaticon:
# https://www.flaticon.com/free-icons/cat
self.setCentralWidget(self.label)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
And if we run it, we should get something like this:

And those are the essential things you should know about QLabels!