PyQt5 – 进度条的名称
在这篇文章中,我们将看到如何设置和获取进度条的名称。进度条的名称基本上是由开发者分配给进度条的一个名称。设置名称是为了对进度条进行分类,因为在设计GUI(图形用户界面)应用程序时,为了对进度条进行分类,会给它们命名,如 “加载”、”下载”、”传输 “等。
为了给进度条设置名称,使用了setAccessibleName方法,为了获得名称,使用了accessibleName方法,如果没有分配名称,accessibleName()方法将返回空白字符串。
语法:
bar.setAccessibleName(name)
bar.accessibleName()
参数:
setAccessibleName以字符串为参数。
accessibleName不需要参数。
返回 :
setAccessibleName返回无。
accessibleName返回字符串。
下面是实现方法
# 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, 200, 30)
# setting the value
bar.setValue(30)
# setting alignment to center
bar.setAlignment(Qt.AlignCenter)
# setting up the name
bar.setAccessibleName("Loading")
# getting the name
name = bar.accessibleName()
# creating label to display the name
label = QLabel("Name of progress bar = " + name, self)
# adjusting the size of label
label.adjustSize()
# changing the position of label
label.move(200, 150)
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :