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 :
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5.QtGui import *
import sys
class Window(QMainWindow):
def __init__( self ):
super ().__init__()
self .setWindowTitle( "Label" )
self .setGeometry( 0 , 0 , 400 , 300 )
self .label_1 = QLabel( "== Normal Label ====" , self )
self .label_1.move( 100 , 100 )
self .label_1.setStyleSheet( "border: 1px solid black;" )
self .label_2 = QLabel( "====== Adjusted label =====" , self )
self .label_2.move( 100 , 150 )
self .label_2.setStyleSheet( "border: 1px solid black;" )
self .label_2.adjustSize()
self .show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App. exec ())
|
Output :
