PyQt5 – 进度条的 format() 方法

PyQt5 – 进度条的 format() 方法

我们可以使用进度条中的setFormat方法来设置格式和显示文本,format方法用于获取进度条的格式。

注意: 进度条的默认格式是’%p%’,即用于打印百分比,所以如果没有设置特定的格式,format方法将返回’%p%’。

语法: bar.format()

参数: 它不需要参数。

返回: 它返回字符串。

下面是这个方法的实现。

# 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
        bar1 = QProgressBar(self)
 
        # setting geometry to progress bar
        bar1.setGeometry(200, 100, 200, 30)
 
        # setting the value
        bar1.setValue(70)
 
        # getting format of bar1
        format1 = bar1.format()
 
        # printing the format1
        print("format of bar 1 : " + format1)
 
        bar2 = QProgressBar(self)
 
        # setting geometry to progress bar
        bar2.setGeometry(200, 200, 200, 30)
 
        # setting the value
        bar2.setValue(50)
 
        # setting text using format
        bar2.setFormat("Geeks")
 
        # getting format of bar2
        format2 = bar2.format()
 
        # printing the format2
        print("format of bar 2 : " + format2)
 
 
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())

输出:

format of bar 1 : %p%
format of bar 2 : Geeks

PyQt5 - 进度条的 format() 方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程