PyQt5 – Get the size of push button
In this article we will see how to get the size of push button. There basically two ways to get the size of push button. Let’s see both of them with examples.
Method 1: By using size
method.
This method will return the QSize object
which will tell the width and height of the push button.
Syntax : button.size()
Argument : It takes no argument.
Return : It returns QSize object.
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()
def UiComponents( self ):
button = QPushButton( "CLICK" , self )
button.setGeometry( 200 , 150 , 150 , 40 )
button.clicked.connect( self .clickme)
print (button.size())
def clickme( self ):
print ( "pressed" )
App = QApplication(sys.argv)
window = Window()
|
Output :
PyQt5.QtCore.QSize(150, 40)
Method 2: By using geometry
or rect
method.
These method will return the QRect object
which will tell the width and height of the push button as well as the position of push button.
Syntax :
button.geometry()
button.rect()
Argument : Both takes no argument.
Return : Both returns QRect object.
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()
def UiComponents( self ):
button = QPushButton( "CLICK" , self )
button.setGeometry( 200 , 150 , 150 , 40 )
button.clicked.connect( self .clickme)
print (button.geometry())
print (button.rect())
def clickme( self ):
print ( "pressed" )
App = QApplication(sys.argv)
window = Window()
|
Output :
PyQt5.QtCore.QRect(200, 150, 150, 40)
PyQt5.QtCore.QRect(0, 0, 150, 40)
Note : Both geometry
and rect
method gives same result for size but the location will be different. geometry
will give location with respect to main window and rect
method will give location with respect to it self.
Last Updated :
22 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...