Issue
This is the part of code where I create the zipfile. Just change the "path" variable for a real path in your computer and it should work just fine ("path" being the name and path of the new zip file). I can create zip files of small directories without any problem, but if I try to do it with a bigger folder, it just freezes until it's done or it just crashes and shuts down. The zipfile is created though, so the problem is only the GUI freezing and crashing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog
import zipfile
import os
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(200, 180, 381, 101))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.createZipFile)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "TEST"))
def createZipFile(self):
directoryname = QFileDialog.getExistingDirectory()
abs_src = os.path.abspath(directoryname)
with zipfile.ZipFile(path,'w') as my_zip2:
for folderName, subfolders, filenames in os.walk(directoryname):
for filename in filenames:
absname = os.path.abspath(os.path.join(folderName, filename))
arcname = absname[len(abs_src) + 1:]
my_zip2.write(absname, arcname)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Solution
I solved this thanks to GPhilo comment. The answer is basically to use Threads. I linked the page I used to learn how to use them in the comments section.
Answered By - Edgedancer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.