PyQt5 - 如何设置进度条的最大值?
在本文中,我们将看到如何设置进度条的最大值。默认情况下,进度条的最大值为 100,但我们可以根据需要更改它。为了做到这一点,我们将使用setMaximum
方法,这将改变进度条的最大值。
注意:当我们使用setValue
方法设置进度条的值时,百分比可能等于也可能不等于传入的值,这取决于进度条的范围。
percentage = ((value_passed - minimum_value)/(maximum_value - minimum_value))*100
这里, minimum_value = 0,value_passed 和 maximum_value 由用户设置。
Syntax : bar.setMaximum(maximum_value)
Argument : It takes argument.
Action performed : It will change the maximum value of progress bar.
下面是实现。
# 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, 150, 200, 30)
# setting maximum value of progress bar to 1000
bar.setMaximum(1000)
# setting value to progress bar
bar.setValue(200)
# setting alignment to centre
bar.setAlignment(Qt.AlignCenter)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :