Answer the question
In order to leave comments, you need to log in
How to unpack downloaded archive into python + pyqt5 GUI application?
Please tell me how to automatically unzip the archive after downloading and hide the download bar
import shutil
import subprocess
import sys
import zipfile
from multiprocessing import process
from PyQt5.QtWidgets import QApplication, QProgressBar, QWidget, QLineEdit, QPushButton, \
QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import QThread, pyqtSignal
import urllib.request
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Downloader(QThread):
# Сигнал о количестве данных (PyQt5)
preprogress = pyqtSignal(float)
progress = pyqtSignal(float)
# fileUrl - url - файла, включая сам файл
# filename - имя файла
def __init__(self, fileUrl, fileName):
QThread.__init__(self)
# Флаг инициализации
self._init = False
self.fileUrl = fileUrl
self.fileName = fileName
def run(self):
# тест на локальных данных, но работать должно и с сетью
urllib.request.urlretrieve(self.fileUrl, self.fileName, self._progress)
def _progress(self, block_num, block_size, total_size):
if not self._init:
self.preprogress.emit(total_size)
self._init = True
# Расчет текущего количества данных
downloaded = block_num * block_size
if downloaded < total_size:
# Отправляем промежуток
self.progress.emit(downloaded)
else:
# Чтобы было 100%
self.progress.emit(total_size)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 600, 300)
self.setWindowTitle('GTO Games')
self.downloader = None
self.fileUrl = QLineEdit('http://site.com/dw/Mods.zip')
self.buttonga = QPushButton('Играть', self)
self.loadButton = QPushButton('Скачать', self)
self.loadButton.clicked.connect(self._loadFile)
hbox = QHBoxLayout()
# hbox.addWidget(self.fileUrl)
hbox.addWidget(self.loadButton)
vbox = QVBoxLayout(self)
vbox.addLayout(hbox)
self.bar = QProgressBar()
vbox.addWidget(self.bar)
def initializeUI(self):
self.setGeometry(100, 100, 600, 200)
self.setWindowTitle('GTO Games')
self.buttonga.clicked.connect(self.exerun)
self.buttonga.move(10, 10)
self.show()
def _loadFile(self):
ar = self.fileUrl.text().split('/')
if len(ar) == 0:
return
fileName = f'_{ar[len(ar) - 1]}'
self._download = Downloader(self.fileUrl.text(), fileName)
# Устанавливаем максимальный размер данных
self._download.preprogress.connect(lambda x: self.bar.setMaximum(round(x)))
# Промежуточный/скачанный размер
self._download.progress.connect(lambda d: self.bar.setValue(round(d)))
self._download.start()
print("D_Start")
self.loadButton.hide()
def exerun(self):
args = [r".\Vintagestory.exe", "--connect=xx.xxx.xxx.xx", "--pw=9029"]
subprocess.call(args)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question