PyQt5 - 自动调整进度条大小
在本文中,我们将看到如何调整进度条的大小。调整大小意味着根据需要设置进度条的大小,不要太大也不要太小,只是可以包含其中所有文本的大小。为此,我们将使用adjustSize
方法。
注意:调整尺寸将设置所需的最小尺寸。它不依赖于使用setFormat
方法添加的文本。
Syntax : bar.adjustSize()
Argument : It takes no argument.
Action performed : It will adjust the size according to the need.
下面是实现。
# 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())
输出 :