PyQt5 – How to change font and size of Label text ?

A label is a graphical control element which displays text on a form. A label is generally used to identify a nearby text box or other widget. Some labels can respond to events such as mouse clicks, allowing the text of the label to be copied, but this is not standard user-interface practice.

In this article, we will see how to change the font and size of the text in Label, we can do this by using setFont() method.

Syntax : label.setFont(QFont(font_name, size))

Argument : It take two argument :
1. Font name it can be ‘Arial’, ‘Times’ etc.
2. Size to be set in integer.

Below is the Python implementation –

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required libraries
  
from PyQt5.QtWidgets import * 
from PyQt5.QtGui import * 
import sys
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
        # set the title
        self.setWindowTitle("Label")
  
        # setting  the geometry of window
        self.setGeometry(0, 0, 400, 300)
  
        # creating a label widget
        # by default label will display at top left corner
        self.label_1 = QLabel('Arial font', self)
  
        # moving position
        self.label_1.move(100, 100)
  
        # setting font and size
        self.label_1.setFont(QFont('Arial', 10))
  
        # creating a label widget
        # by default label will display at top left corner
        self.label_2 = QLabel('Times font', self)
  
        # moving position
        self.label_2.move(100, 120)
  
        # setting font and size
        self.label_2.setFont(QFont('Times', 10))
  
  
        # show all the widgets
        self.show()
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

chevron_right


Output :
pyqt-change-font-Qlabel




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.