GUI not response when click button
-
I made a program that allows you to get comics from the internet. When I run the code below, my GUI does not respond until the download is complete. I'm not sure what the issue with threads in my code is.
class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(947, 606) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.tabWidget = QtWidgets.QTabWidget(self.centralwidget) self.tabWidget.setEnabled(True) self.tabWidget.setGeometry(QtCore.QRect(0, 0, 1571, 951)) font = QtGui.QFont() font.setFamily("Rockwell Condensed") font.setPointSize(10) font.setBold(True) font.setWeight(75) self.tabWidget.setFont(font) self.tabWidget.setObjectName("tabWidget") self.tab = QtWidgets.QWidget() self.tab.setObjectName("tab") self.groupBox_3 = QtWidgets.QGroupBox(self.tab) self.groupBox_3.setGeometry(QtCore.QRect(10, 140, 371, 281)) self.groupBox_3.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter) self.groupBox_3.setObjectName("groupBox_3") self.frame = QtWidgets.QFrame(self.groupBox_3) self.frame.setGeometry(QtCore.QRect(10, 20, 111, 101)) self.frame.setFrameShape(QtWidgets.QFrame.WinPanel) self.frame.setFrameShadow(QtWidgets.QFrame.Raised) self.frame.setObjectName("frame") self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.frame) self.verticalLayout_3.setObjectName("verticalLayout_3") self.label_2 = QtWidgets.QLabel(self.frame) self.label_2.setAutoFillBackground(True) self.label_2.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_2.setText("") self.label_2.setPixmap(QtGui.QPixmap("logo/Inkedvcomic_LI.jpg")) self.label_2.setScaledContents(True) self.label_2.setObjectName("label_2") self.verticalLayout_3.addWidget(self.label_2) self.frame_2 = QtWidgets.QFrame(self.groupBox_3) self.frame_2.setGeometry(QtCore.QRect(130, 20, 101, 101)) self.frame_2.setFrameShape(QtWidgets.QFrame.WinPanel) self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised) self.frame_2.setObjectName("frame_2") self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.frame_2) self.verticalLayout_4.setObjectName("verticalLayout_4") self.label_3 = QtWidgets.QLabel(self.frame_2) self.label_3.setAutoFillBackground(True) self.label_3.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_3.setText("") self.label_3.setPixmap(QtGui.QPixmap("logo/NHENTAI.jpg")) self.label_3.setScaledContents(True) self.label_3.setObjectName("label_3") self.verticalLayout_4.addWidget(self.label_3) self.frame_3 = QtWidgets.QFrame(self.groupBox_3) self.frame_3.setGeometry(QtCore.QRect(240, 20, 101, 101)) self.frame_3.setFrameShape(QtWidgets.QFrame.WinPanel) self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised) self.frame_3.setObjectName("frame_3") self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.frame_3) self.verticalLayout_5.setObjectName("verticalLayout_5") self.label_4 = QtWidgets.QLabel(self.frame_3) self.label_4.setAutoFillBackground(True) self.label_4.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_4.setText("") self.label_4.setPixmap(QtGui.QPixmap("logo/truyen86.png")) self.label_4.setScaledContents(True) self.label_4.setObjectName("label_4") self.verticalLayout_5.addWidget(self.label_4) self.frame_4 = QtWidgets.QFrame(self.groupBox_3) self.frame_4.setGeometry(QtCore.QRect(240, 140, 101, 101)) self.frame_4.setFrameShape(QtWidgets.QFrame.WinPanel) self.frame_4.setFrameShadow(QtWidgets.QFrame.Raised) self.frame_4.setObjectName("frame_4") self.verticalLayout_6 = QtWidgets.QVBoxLayout(self.frame_4) self.verticalLayout_6.setObjectName("verticalLayout_6") self.label_5 = QtWidgets.QLabel(self.frame_4) self.label_5.setAutoFillBackground(True) self.label_5.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_5.setText("") self.label_5.setPixmap(QtGui.QPixmap("logo/truyentranhaudio.png")) self.label_5.setScaledContents(True) self.label_5.setObjectName("label_5") self.verticalLayout_6.addWidget(self.label_5) self.frame_6 = QtWidgets.QFrame(self.groupBox_3) self.frame_6.setGeometry(QtCore.QRect(130, 140, 101, 101)) self.frame_6.setFrameShape(QtWidgets.QFrame.WinPanel) self.frame_6.setFrameShadow(QtWidgets.QFrame.Raised) self.frame_6.setObjectName("frame_6") self.verticalLayout_8 = QtWidgets.QVBoxLayout(self.frame_6) self.verticalLayout_8.setObjectName("verticalLayout_8") self.label_7 = QtWidgets.QLabel(self.frame_6) self.label_7.setAutoFillBackground(True) self.label_7.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_7.setText("") self.label_7.setPixmap(QtGui.QPixmap("logo/nhattruyenhay.png")) self.label_7.setScaledContents(True) self.label_7.setObjectName("label_7") self.verticalLayout_8.addWidget(self.label_7) self.frame_5 = QtWidgets.QFrame(self.groupBox_3) self.frame_5.setGeometry(QtCore.QRect(10, 140, 111, 101)) self.frame_5.setFrameShape(QtWidgets.QFrame.WinPanel) self.frame_5.setFrameShadow(QtWidgets.QFrame.Raised) self.frame_5.setObjectName("frame_5") self.verticalLayout_7 = QtWidgets.QVBoxLayout(self.frame_5) self.verticalLayout_7.setObjectName("verticalLayout_7") self.label_6 = QtWidgets.QLabel(self.frame_5) self.label_6.setAutoFillBackground(True) self.label_6.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_6.setText("") self.label_6.setPixmap(QtGui.QPixmap("logo/nettruyenvip.png")) self.label_6.setScaledContents(True) self.label_6.setObjectName("label_6") self.verticalLayout_7.addWidget(self.label_6) self.label_8 = QtWidgets.QLabel(self.groupBox_3) self.label_8.setGeometry(QtCore.QRect(20, 120, 81, 16)) self.label_8.setAutoFillBackground(False) self.label_8.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_8.setScaledContents(True) self.label_8.setAlignment(QtCore.Qt.AlignCenter) self.label_8.setObjectName("label_8") self.label_9 = QtWidgets.QLabel(self.groupBox_3) self.label_9.setGeometry(QtCore.QRect(160, 120, 51, 15)) self.label_9.setAutoFillBackground(False) self.label_9.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_9.setScaledContents(True) self.label_9.setObjectName("label_9") self.label_10 = QtWidgets.QLabel(self.groupBox_3) self.label_10.setGeometry(QtCore.QRect(260, 120, 61, 16)) self.label_10.setAutoFillBackground(False) self.label_10.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_10.setScaledContents(True) self.label_10.setAlignment(QtCore.Qt.AlignCenter) self.label_10.setObjectName("label_10") self.label_11 = QtWidgets.QLabel(self.groupBox_3) self.label_11.setGeometry(QtCore.QRect(20, 250, 91, 16)) self.label_11.setAutoFillBackground(False) self.label_11.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_11.setScaledContents(True) self.label_11.setAlignment(QtCore.Qt.AlignCenter) self.label_11.setObjectName("label_11") self.label_12 = QtWidgets.QLabel(self.groupBox_3) self.label_12.setGeometry(QtCore.QRect(130, 250, 101, 16)) self.label_12.setAutoFillBackground(False) self.label_12.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_12.setScaledContents(True) self.label_12.setAlignment(QtCore.Qt.AlignCenter) self.label_12.setObjectName("label_12") self.label_13 = QtWidgets.QLabel(self.groupBox_3) self.label_13.setGeometry(QtCore.QRect(230, 250, 121, 16)) self.label_13.setAutoFillBackground(False) self.label_13.setFrameShape(QtWidgets.QFrame.NoFrame) self.label_13.setScaledContents(True) self.label_13.setAlignment(QtCore.Qt.AlignCenter) self.label_13.setObjectName("label_13") self.widget = QtWidgets.QWidget(self.tab) self.widget.setGeometry(QtCore.QRect(10, 0, 921, 141)) self.widget.setObjectName("widget") self.verticalLayout = QtWidgets.QVBoxLayout(self.widget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setObjectName("verticalLayout") self.groupBox = QtWidgets.QGroupBox(self.widget) self.groupBox.setObjectName("groupBox") self.label = QtWidgets.QLabel(self.groupBox) self.label.setGeometry(QtCore.QRect(10, 20, 131, 31)) self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setObjectName("label") self.textEdit = QtWidgets.QTextEdit(self.groupBox) self.textEdit.setGeometry(QtCore.QRect(140, 20, 611, 31)) self.textEdit.setObjectName("textEdit") self.DOWNLOADBUTTON = QtWidgets.QPushButton(self.groupBox) self.DOWNLOADBUTTON.setGeometry(QtCore.QRect(754, 20, 111, 31)) font = QtGui.QFont() font.setFamily("ROG Fonts") font.setPointSize(10) font.setBold(False) font.setWeight(50) self.DOWNLOADBUTTON.setFont(font) self.DOWNLOADBUTTON.setObjectName("DOWNLOADBUTTON") self.verticalLayout.addWidget(self.groupBox) self.groupBox_2 = QtWidgets.QGroupBox(self.widget) self.groupBox_2.setObjectName("groupBox_2") self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.groupBox_2) self.verticalLayout_2.setObjectName("verticalLayout_2") self.LOAD = QtWidgets.QLabel(self.groupBox_2) self.LOAD.setAlignment(QtCore.Qt.AlignCenter) self.LOAD.setObjectName("LOAD") self.verticalLayout_2.addWidget(self.LOAD) self.TRANG_THAI = QtWidgets.QLabel(self.groupBox_2) self.TRANG_THAI.setAlignment(QtCore.Qt.AlignCenter) self.TRANG_THAI.setObjectName("TRANG_THAI") self.verticalLayout_2.addWidget(self.TRANG_THAI) self.verticalLayout.addWidget(self.groupBox_2) self.tabWidget.addTab(self.tab, "") self.tab_2 = QtWidgets.QWidget() self.tab_2.setObjectName("tab_2") self.label_15 = QtWidgets.QLabel(self.tab_2) self.label_15.setGeometry(QtCore.QRect(290, 70, 361, 91)) font = QtGui.QFont() font.setFamily("Rockwell Condensed") font.setPointSize(26) font.setBold(True) font.setWeight(75) self.label_15.setFont(font) self.label_15.setStyleSheet("") self.label_15.setAlignment(QtCore.Qt.AlignCenter) self.label_15.setObjectName("label_15") self.label_16 = QtWidgets.QLabel(self.tab_2) self.label_16.setGeometry(QtCore.QRect(890, 240, 51, 51)) self.label_16.setAutoFillBackground(False) self.label_16.setStyleSheet("background-color: rgb(255, 255, 255);") self.label_16.setText("") self.label_16.setPixmap(QtGui.QPixmap("logo/lock.png")) self.label_16.setScaledContents(True) self.label_16.setObjectName("label_16") self.widget1 = QtWidgets.QWidget(self.tab_2) self.widget1.setGeometry(QtCore.QRect(10, 240, 871, 51)) self.widget1.setObjectName("widget1") self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget1) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setObjectName("horizontalLayout") self.textEdit_2 = QtWidgets.QTextEdit(self.widget1) self.textEdit_2.setObjectName("textEdit_2") self.horizontalLayout.addWidget(self.textEdit_2) self.pushButton = QtWidgets.QPushButton(self.widget1) self.pushButton.setAutoFillBackground(False) self.pushButton.setStyleSheet("background-color: rgb(255, 255, 255);") self.pushButton.setObjectName("pushButton") self.horizontalLayout.addWidget(self.pushButton) self.tabWidget.addTab(self.tab_2, "") self.tab_3 = QtWidgets.QWidget() self.tab_3.setObjectName("tab_3") self.label_14 = QtWidgets.QLabel(self.tab_3) self.label_14.setGeometry(QtCore.QRect(0, 0, 941, 571)) self.label_14.setText("") self.label_14.setPixmap(QtGui.QPixmap("logo/Ảnh chụp màn hình 2021-08-06 145733 (2).jpg")) self.label_14.setScaledContents(True) self.label_14.setObjectName("label_14") self.tabWidget.addTab(self.tab_3, "") MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) self.tabWidget.setCurrentIndex(1) QtCore.QMetaObject.connectSlotsByName(MainWindow) self.DOWNLOADBUTTON.clicked.connect(self.load) self.DOWNLOADBUTTON.clicked.connect(self.command) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.groupBox_3.setTitle(_translate("MainWindow", "WEB HỖ TRỢ")) self.label_8.setText(_translate("MainWindow", "VCOMIC")) self.label_9.setText(_translate("MainWindow", "NHENTAI")) self.label_10.setText(_translate("MainWindow", "TRUYEN86")) self.label_11.setText(_translate("MainWindow", "NETTRUYENVIP")) self.label_12.setText(_translate("MainWindow", "NHATRUYENHAY")) self.label_13.setText(_translate("MainWindow", "TRUYENTRANHAUDIO")) self.groupBox.setTitle(_translate("MainWindow", "LINK")) self.label.setText(_translate("MainWindow", "NHẬP LINK TRUYỆN:")) self.DOWNLOADBUTTON.setText(_translate("MainWindow", "DOWNLOAD")) self.groupBox_2.setTitle(_translate("MainWindow", "TRẠNG THÁI")) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "DOWN TRUYỆN")) self.label_15.setText(_translate("MainWindow", "NHẬP KEY VIP")) self.pushButton.setText(_translate("MainWindow", "UNLOCK")) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "KEY VIP")) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "TÁC GIẢ")) def load(self,MainWindow): _translate = QtCore.QCoreApplication.translate self.LOAD.setText(_translate("MainWindow", "Đã tải xong")) def command(self): l = [] session = HTMLSession() #LINK TRUYỆN url = self.textEdit.toPlainText() if "nettruyenvip" in url: r = session.get(url) rs = r.html.find("#nt_listchapter .chapter a", first = False) for i in reversed(rs): #LINK CHAP c_url = i.attrs['href'] r_c = session.get(c_url) rs_c = r_c.html.find(".page-chapter img", first = False) #TÊN FOLDER fol = c_url.split("/")[-2] #TÊN TRUYỆN truyen = c_url.split("/")[-3] #LINK ẢNH for x in rs_c: img = "http:"+x.attrs['src'] i_url = img.split("?data=net")[0] filen = i_url.split("/")[-1] #TẠO FOLDER CHAP Path("C:/Users/ASUS/OneDrive/Hình ảnh/Saved Pictures/" + truyen + "/" + fol).mkdir(parents = True, exist_ok = True) #response headers = { "Referer": "http://www.nettruyenvip.com/", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36" } response = requests.request("GET", i_url, headers=headers, stream = True) #TẢI ẢNH TRONG CHAP response.raw.decode_content = True file_i = open("C:/Users/ASUS/OneDrive/Hình ảnh/Saved Pictures/" + truyen + "/" + fol + "/" + filen, "wb") shutil.copyfileobj(response.raw, file_i) #TẠO FILE PDF l.append(fol) for n in l: with open(fol + ".pdf","wb") as f: f.write(img2pdf.convert(glob.glob("C:/Users/ASUS/OneDrive/Hình ảnh/Saved Pictures/" + truyen + "/" + n +"/*.jpg"))) #print("----------------------------FINISH---------------------------------") elif "nhentai" in url: r = session.get(url) rs = r.html.find(".thumb-container a", first = False) for i in rs: c_url = "https://nhentai.net" + i.attrs['href'] r_c = session.get(c_url) rs_c = r_c.html.find("#image-container a img", first = False) #TÊN TRUYỆN truyen = url.split("/")[-2] for x in rs_c: img = x.attrs['src'] i_url = img filen = i_url.split("/")[-1] #TẠO FOLDER CHAP Path("C:/Users/ASUS/OneDrive/Hình ảnh/Saved Pictures/" + truyen).mkdir(parents = True, exist_ok = True) #response headers = {"sec-ch-ua": "^\^Chromium^^;v=^\^92^^, ^\^"} response = requests.request("GET", i_url, headers=headers, stream = True) #TẢI ẢNH TRONG CHAP response.raw.decode_content = True file_i = open("C:/Users/ASUS/OneDrive/Hình ảnh/Saved Pictures/" + truyen + "/" + filen, "wb") shutil.copyfileobj(response.raw, file_i) #TẠO FILE PDF with open(truyen + ".pdf","wb") as f: f.write(img2pdf.convert(glob.glob("C:/Users/ASUS/OneDrive/Hình ảnh/Saved Pictures/"+ truyen +"/*.jpg"))) _translate = QtCore.QCoreApplication.translate self.TRANG_THAI.setText(_translate("MainWindow", "ĐÃ TẢI XONG 1/1 CHAP")) #print("----------------------------FINISH---------------------------------")
-
Hi and welcome to devnet,
You are using requests to do your downloading. It does it synchronously hence it blocks the Qt event loop. Either use QNetworkAccessManager or move your download code in a different thread.
-
-
If I transfer the code to a separate thread, where should I place it, and do you have any additional Python links?
-
@SeaLongHoang With Qt you do not need any additional threads as it is asynchronous. Please check the links I provided - you will see that there are no threads involved.
-
@SeaLongHoang Neither QNetworkAccessManager nor python requests library are used to parse HTML, for this you must use Beautiful Soup library.