PyQt5 – 如何更改文本进度条的样式和大小?
在本文中,我们将了解如何更改进度条内数据的字体样式和大小。我们可以使用setFormat
方法设置进度条中的文本。为了改变字体和大小,我们将使用以 QFont 对象为参数的setFont
方法,字体和大小将被改变。
Syntax : bar.setFont(QFont(font_name, size))
Argument : It takes QFont object as argument.
Action performed : This will change the font and size of the text.
下面是实现。
# 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)
# setting alignment to center
bar1.setAlignment(Qt.AlignCenter)
# setting font and the size
bar1.setFont(QFont('Arial', 15))
# creating progress bar
bar2 = QProgressBar(self)
# setting geometry to progress bar
bar2.setGeometry(200, 200, 200, 30)
# setting the value
bar2.setValue(20)
# adding text using setFormat
bar2.setFormat("Hello Geeks")
# setting alignment to center
bar2.setAlignment(Qt.AlignCenter)
# setting font and the size
bar2.setFont(QFont('Times', 7))
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :