在pyqt5中实现进度条下载git repo,可以通过以下步骤实现:
import sys
import os
import subprocess
from PyQt5.QtWidgets import QApplication, QMainWindow, QProgressBar, QPushButton, QLabel
from PyQt5.QtCore import Qt, QThread, pyqtSignal
class GitCloneThread(QThread):
progress_updated = pyqtSignal(int)
def __init__(self, repo_url, save_path):
super().__init__()
self.repo_url = repo_url
self.save_path = save_path
def run(self):
cmd = ['git', 'clone', self.repo_url, self.save_path]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
total_bytes = 0
downloaded_bytes = 0
while True:
line = process.stdout.readline().decode().strip()
if not line:
break
if 'Receiving objects' in line:
total_bytes = int(line.split()[2])
if 'Receiving objects' in line or 'Resolving deltas' in line:
downloaded_bytes += len(line)
progress = int(downloaded_bytes / total_bytes * 100)
self.progress_updated.emit(progress)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Git Repo Downloader')
self.setGeometry(100, 100, 400, 150)
self.progress_bar = QProgressBar(self)
self.progress_bar.setGeometry(30, 40, 340, 25)
self.download_button = QPushButton('Download', self)
self.download_button.setGeometry(30, 80, 100, 30)
self.download_button.clicked.connect(self.start_download)
self.status_label = QLabel(self)
self.status_label.setGeometry(150, 80, 220, 30)
self.status_label.setAlignment(Qt.AlignCenter)
def start_download(self):
repo_url = 'https://github.com/your/repo.git'
save_path = '/path/to/save/repo'
self.download_button.setEnabled(False)
self.git_thread = GitCloneThread(repo_url, save_path)
self.git_thread.progress_updated.connect(self.update_progress)
self.git_thread.finished.connect(self.download_finished)
self.git_thread.start()
def update_progress(self, progress):
self.progress_bar.setValue(progress)
self.status_label.setText(f'Downloading... {progress}%')
def download_finished(self):
self.download_button.setEnabled(True)
self.status_label.setText('Download finished!')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
这样,当点击"Download"按钮时,程序将在后台执行git clone命令,并通过进度条显示下载进度。下载完成后,进度条将显示100%,并显示"Download finished!"的状态。
推荐的腾讯云相关产品:腾讯云对象存储(COS),用于存储和管理下载的git repo文件。产品介绍链接地址:https://cloud.tencent.com/product/cos
领取专属 10元无门槛券
手把手带您无忧上云