PyQt5 – Adjust the size of push button according to the text
Last Updated :
22 Apr, 2020
In this article we will see how to adjust the size of the push button according to the text. We know we can change the size of button using resize
method every time resizing it manually is difficult. Therefore, we have adjustSize
method which automatically adjust the size of the push button according to the text in it.
Syntax : button.adjustSize()
Argument : It takes no argument.
Action performed : It changes the size of button according to the text in it.
Code :
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__( self ):
super ().__init__()
self .setWindowTitle( "Python " )
self .setGeometry( 100 , 100 , 600 , 400 )
self .UiComponents()
self .show()
def UiComponents( self ):
button = QPushButton( "CLICK" , self )
button.setGeometry( 200 , 150 , 200 , 40 )
button.adjustSize()
button.clicked.connect( self .clickme)
def clickme( self ):
print ( "pressed" )
App = QApplication(sys.argv)
window = Window()
sys.exit(App. exec ())
|
Output :
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...