PyQt5 – 自动调整进度条的大小
在这篇文章中,我们将看到如何调整进度条的大小。调整大小是指根据需要设置进度条的大小,既不能太大,也不能太小,只要能包含其中的所有文字即可。为了做到这一点,我们将使用调整尺寸方法。
注意: 调整尺寸将设置最小的所需尺寸。它并不取决于使用setFormat方法添加的文本。
语法: bar.adjustsize()
参数: 它不需要参数。
执行的操作: 它将根据需要调整大小。
下面是实现方法。
# 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())
输出 :