PyQt5 – 如何改变进度条的样式和大小

PyQt5 – 如何改变进度条的样式和大小

在这篇文章中,我们将看到如何改变进度条中数据的字体风格和大小。我们可以使用setFormat方法设置进度条中的文本。为了改变字体和大小,我们将使用setFont方法,该方法以QFont对象为参数,字体和大小将被改变。

语法: bar.setFont(QFont(font_name, size))

参数: 它以QFont对象作为参数。

执行的动作: 这将改变文本的字体和大小。

下面是具体的实现。

# 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())

输出 :

PyQt5 - 如何改变进度条的样式和大小?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程