Open In App

PyQt5 – Get the size of push button

Improve
Improve
Like Article
Like
Save
Share
Report

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 :




# 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()


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 :




# 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()


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
Previous
Next
Share your thoughts in the comments
Similar Reads