PyQt5是开发Python GUI 桌面应用程序的新兴 GUI 库之一。它具有丰富而强大的功能,可确保生产质量的应用程序。学习 PyQt5 库是您知识的补充。您可以开发您的消费者质量、高度专业的应用程序。
在本文中,我们将学习如何在 PyQt5 中自动化进度条。自动化我们的意思是动态更改和设置进度条的值。假设您正在通过 Internet 下载任何文件并希望显示下载进度,那么这篇文章肯定会对您有所帮助。
在本示例中,我们使用U rllib 下载文件的库是使用Python下载文件的最常用库。
Syntax :
self.progressBar = QProgressBar(self)
QProgressBar class is for creating the progress bar object.
首先,通过下面的代码,然后我们将解释整个事情的作用。
代码 :
Python3
# importing libraries
import urllib.request
from PyQt5.QtWidgets import *
import sys
class GeeksforGeeks(QWidget):
def __init__(self):
super().__init__()
# calling a defined method to initialize UI
self.init_UI()
# method for creating UI widgets
def init_UI(self):
# creating progress bar
self.progressBar = QProgressBar(self)
# setting its size
self.progressBar.setGeometry(25, 45, 210, 30)
# creating push button to start download
self.button = QPushButton('Start', self)
# assigning position to button
self.button.move(50, 100)
# assigning activity to push button
self.button.clicked.connect(self.Download)
# setting window geometry
self.setGeometry(310, 310, 280, 170)
# setting window action
self.setWindowTitle("GeeksforGeeks")
# showing all the widgets
self.show()
# when push button is pressed, this method is called
def Handle_Progress(self, blocknum, blocksize, totalsize):
## calculate the progress
readed_data = blocknum * blocksize
if totalsize > 0:
download_percentage = readed_data * 100 / totalsize
self.progressBar.setValue(download_percentage)
QApplication.processEvents()
# method to download any file using urllib
def Download(self):
# specify the url of the file which is to be downloaded
down_url = '' # specify download url here
# specify save location where the file is to be saved
save_loc = 'C:\Desktop\GeeksforGeeks.png'
# Downloading using urllib
urllib.request.urlretrieve(down_url,save_loc, self.Handle_Progress)
# main method to call our app
if __name__ == '__main__':
# create app
App = QApplication(sys.argv)
# create the instance of our window
window = GeeksforGeeks()
# start the app
sys.exit(App.exec())
解释 :
下面是urllib的语法,我们必须研究它需要的所有参数。
Synatx: urllib.request.urlretrieve(url, filename, reporthook)
Parameters: This method will take following parameters :
The first parameter is the url of the file, which is to be downloaded.
The second parameter, if present, specifies the file location to save the file (if this argument is not passed, the location will be a temp file with an auto-generated name).
The third parameter is a callable that will be called when the file is being downloaded and once after another, each block would be read. The callable (which is a function Handle_Progress in this case) will be passed as three arguments :
- a count of blocks transferred so far (blocknum)
- block size in bytes (blocksize)
- the total size of the file (totalsize)
函数Handle_Progress因此接收三个参数。当前下载的文件大小是通过blocknum和blocksize相乘动态计算出来的,并存储在变量readed_data中。
其余的工作由计算百分比的公式完成。我们将 readed_data 乘以 100,然后除以文件的总大小。它为我们提供了当前的下载百分比。然后我们使用 progressBar 对象的 setValue() 方法将此下载百分比设置到进度条。
self.progressBar.setValue(download_percentage)
输出 :