import sys
from io import BytesIO
from time import sleep
from PyQt5.QtWidgets import (QWidget, QProgressBar, QDialog,
QPushButton, QApplication, QVBoxLayout, QLabel, QMessageBox)
from PyQt5.QtCore import QTimer, QThread, pyqtSignal, Qt
from PyQt5.QtGui import QPixmap, QImage
import requests
from PIL import Image
from PIL.ImageQt import ImageQt
# Отловить ошибки в слотах PyQt5
def log_uncaught_exceptions(ex_cls, ex, tb):
text = '{}: {}:\n'.format(ex_cls.__name__, ex)
import traceback
text += ''.join(traceback.format_tb(tb))
print(text)
QMessageBox.critical(None, 'Error', text)
quit()
sys.excepthook = log_uncaught_exceptions
class ProgressWindow(QThread):
signal = pyqtSignal(int)
chunk_data = pyqtSignal(bytes)
url = "http://risovach.ru/upload/2014/02/mem/muzhik-bleat_43233947_orig_.jpg"
headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
def __init__(self):
super().__init__()
self.window = QDialog()
self.window.setWindowTitle('Progress window')
self.progress = QProgressBar(self.window)
self.progress.setGeometry(0, 0, 240, 25)
def download_file(self, url):
with requests.get(url, headers=self.headers, stream=True) as response:
response.raise_for_status()
file_size = response.headers['content-length'] # 42689
print(file_size)
step = 0
for chunk in response.iter_content(chunk_size=426):
sleep(0.01)
step = step + 1
self.signal.emit(step)
self.chunk_data.emit(chunk)
self.signal.emit(0)
def run(self):
try:
self.download_file(self.url)
except Exception as error:
print(error)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 500, 350)
self.setWindowTitle('Как научиться нормально программировать')
# self.setWindowModality(Qt.WindowModal)
vbox = QVBoxLayout(self)
self.start_button = QPushButton('Узнать', self)
self.start_button.move(40, 80)
self.start_button.clicked.connect(self.download)
self.label_image = QLabel(self)
vbox.addWidget(self.label_image)
vbox.addWidget(self.start_button)
self.setLayout(vbox)
self.progress_window = ProgressWindow()
self.progress_window.window.setModal(True)
self.buffer = BytesIO()
self.chunk_size = 0
def download(self):
self.progress_window.window.show()
self.progress_window.start()
self.start_button.setEnabled(False)
self.progress_window.signal.connect(self.update_progress)
self.progress_window.chunk_data.connect(self.image_view)
def update_progress(self, step):
if step >= 101:
self.progress_window.window.hide()
self.progress_window.terminate()
self.start_button.setEnabled(True)
else:
self.progress_window.progress.setValue(step)
def image_view(self, chunk):
self.buffer.write(chunk)
if len(chunk) <= 89:
pillow_image = Image.open(self.buffer)
pixmap = QPixmap(pillow_image.toqpixmap())
self.label_image.setPixmap(pixmap)
self.label_image.update()
if __name__ == '__main__':
application = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(application.exec_())