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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
  
        # calling method
        self.UiComponents()
  
  
  
    # method for widgets
    def UiComponents(self):
  
        # creating a push button
        button = QPushButton("CLICK", self)
  
        # setting geometry of button
        # rectangular shape i.e width > height
        button.setGeometry(200, 150, 150, 40)
  
        # adding action to a button
        button.clicked.connect(self.clickme)
  
        # printing button size
        print(button.size())
  
  
  
    # action method
    def clickme(self):
  
        # printing pressed
        print("pressed")
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
  
        # calling method
        self.UiComponents()
  
  
  
    # method for widgets
    def UiComponents(self):
  
        # creating a push button
        button = QPushButton("CLICK", self)
  
        # setting geometry of button
        # rectangular shape i.e width > height
        button.setGeometry(200, 150, 150, 40)
  
        # adding action to a button
        button.clicked.connect(self.clickme)
  
        # printing button size
        print(button.geometry())
        print(button.rect())
  
  
  
    # action method
    def clickme(self):
  
        # printing pressed
        print("pressed")
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()

chevron_right


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.




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.