I am trying to download image through web scrapping through gui, whenever i run script images are downloaded but gui window doesnt respond and keep loading and after all images are downloaded it gets its control back plus my search button completely get frozen once i click it and it start to download images
index.py
from PyQt6.QtWidgets import (
QApplication,
QWidget,
QMainWindow,
QLineEdit,
QVBoxLayout,
QHBoxLayout,
QPushButton,
QGridLayout,
QLabel
)
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QCursor
import sys
from IMD import Parser
from pathlib import Path
import os
os.chdir("./images")
class ImageDownloaderWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Layout")
self.window = QWidget()
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignmentFlag.AlignTop)
self.window.setLayout(self.layout)
self.setFixedSize(550, 500)
self.setCentralWidget(self.window)
self.createText("Image Downloader", self.layout, """
margin: 80px 20px 10px 20px;
font-size: 40px;
font-family: monospace;
font-weight: bold;
""")
self.createText("Download Images of your choice", self.layout, """
margin-bottom: 80px;
font-size: 20px;
font-family: 'Eras Bold ITC';
""")
self.name = ""
self.createSearchBar()
self.createSearchButton()
self.searchClickEvent()
def createText(self, text, layout, stylesheet=None):
self.display = QLabel(text)
self.display.setStyleSheet(stylesheet)
self.display.setAlignment(Qt.AlignmentFlag.AlignHCenter)
layout.addWidget(self.display)
def createSearchBar(self):
self.searchBar = QLineEdit()
# self.searchBar.setStyleSheet("margin: 0px 50px; color: #fff;")
self.searchBar.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.layout.addWidget(self.searchBar)
def createSearchButton(self):
self.searchButton = QPushButton("Search")
self.searchButton.setFixedSize(140, 60)
self.searchButton.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
self.layout.addWidget(self.searchButton, alignment=Qt.AlignmentFlag.AlignCenter)
def displayImageName(self):
return self.searchBar.text()
def searchClickEvent(self):
self.searchButton.clicked.connect(self.parsedImages)
def parsedImages(self):
self.name = self.displayImageName()
parser = Parser("https://unsplash.com/s/photos/"+self.name)
Path(self.name).mkdir(exist_ok=True)
parser.getSources()
os.chdir(self.name)
imgsLen = len(os.listdir())
images = parser.srcs
for index, img in enumerate(images):
with open(self.name+str(index)+'.jpg', "wb") as f:
f.write(parser.getContent(img))
def main():
app = QApplication([])
app.setStyleSheet(
"""
QWidget {
color: #fff;
background-color: #123;
}
QLineEdit {
color: #000;
padding: 10px;
border: 1px solid #000;
border-radius: 10px;
background-color: #fff;
margin: 0px 50px;
font-size: 15pt;
}
QPushButton {
background-color: #00a656;
margin-top: 10px;
padding: 250px;
border: 1px solid #00a656;
border-radius: 10px;
font-size: 23px;
font-weight: bold;
letter-spacing: 3px;
}
QPushButton:hover {
cursor: pointer;
background-color: #000;
color: #fff;
}
"""
)
ui = ImageDownloaderWindow()
ui.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()
also i have made my own parser class to get image of content #parser.py
import requests
from bs4 import BeautifulSoup
from pathlib import Path
import os
class Parser:
def __init__(self, site):
self.site = site
self.srcs = []
def parse_content(self):
soup = None
try:
response = requests.get(self.site)
soup = BeautifulSoup(response.content, "html.parser")
except Exception as error:
soup = None
print(error)
finally:
return soup
def getSources(self):
siteContent = self.parse_content()
if siteContent:
imgElements = siteContent.find_all("img", class_="YVj9w")
for img in imgElements:
self.srcs.append(img.get("src"))
return True
return False
def getContent(self, img):
response = requests.get(img)
return response.content
how should i make my gui window and betton working when it is downloading image. I quit tkinter because i faced same issue there and i thought there was problem with tkinter but now i knew gui behaves in such way and it may have some solution.