Open In App

PyQt5 – Auto Adjusting Progress Bar size

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will see how to adjust the size of progress bar. Adjusting size means setting the size of progress bar as much as it is needed, nor too big nor too small, just the size which can comprise all the text in it. In order to do this we will use adjustSize method. Note : Adjusting size will set the minimum required size. It doesn’t depend on the text added using setFormat method.

Syntax : bar.adjustSize() Argument : It takes no argument. Action performed : It will adjust the size according to the need.

Below is the implementation. 

Python3




# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
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()
 
        # showing all the widgets
        self.show()
 
    # method for widgets
    def UiComponents(self):
        # creating progress bar
        bar = QProgressBar(self)
 
        # setting geometry to progress bar
        bar.setGeometry(200, 100, 20, 50)
 
        # setting the value
        bar.setValue(100)
         
        # setting alignment to center
        bar.setAlignment(Qt.AlignCenter)
 
        # adjusting the size of the progress bar
        bar.adjustSize()
 
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())


Output :



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads