During the designing of the GUI (Graphical User Interface) application there is a need to display plain text as information where label is used, but sometimes information text could be large or much smaller and it is difficult to use resize() method so have to auto adjust the size of the label according to the text, in order to do so adjustSize() method can be used.
adjustSize() method will change the size of label according to the length of the text, if the length is less it will decrease the length and height of the widget and vice versa.
Syntax : label.adjustSize()
Argument : It takes no argument.
Code :
# importing the required libraries from PyQt5.QtWidgets import * from PyQt5 import QtCore 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 self.label_1 = QLabel("== Normal Label ====", self) # moving position self.label_1.move(100, 100) # setting up border self.label_1.setStyleSheet("border: 1px solid black;") # creating a label widget self.label_2 = QLabel("====== Adjusted label =====", self) # moving position self.label_2.move(100, 150) # setting up border self.label_2.setStyleSheet("border: 1px solid black;") # adjusting the size of label self.label_2.adjustSize() # 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()) |
Output :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- PyQt5 – How to change size of the Label | label.resize method
- PyQt5 – How to hide label | label.setHidden method
- PyQt5 Scrollable Label - Setting tool tip to the label part
- PyQt5 Scrollable Label - Getting tool tip text of the label part
- PyQt5 Scrollable Label – Setting tool tip duration to label part
- PyQt5 Scrollable Label – Getting tool tip duration of the label part
- PyQt5 Label – Checking if label is window type
- PyQt5 Label – Checking if label is widget type
- PyQt5 QLabel - Checking if the opacity effect is window type
- PyQt5 – How to add border on QLabel ?
- PyQt5 QLabel - Disabling the color effect according to the user
- PyQt5 QLabel - Accessing blur radius of the blur effect
- PyQt5 QLabel - Setting blur radius to the blur effect
- PyQt5 QLabel - Adding blur effect
- PyQt5 QLabel – Checking if the color effect is window type
- PyQt5 QLabel - Checking if the color effect is widget type
- PyQt5 QLabel - Checking if the color effect is disabled or not
- PyQt5 QLabel - Disabling the blur effect according to the user
- PyQt5 QLabel - Checking if the blur effect is widget type
- PyQt5 QLabel - Checking if the blur effect is window type
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.

