就开发Python GUI桌面应用程序而言, PyQt5是新兴的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'
# Dowloading 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)
输出 :