lundi 14 septembre 2020

How do I delete an entire folder and its content using a QCheckBox using PySide2

So this is not the whole code, but I am having troubles deleting a directory using the checkbox. Inside the widget, there is checkbox, label, lineedit and a button. The checkbox disables and enables the label,lineedit and button. When the checkbox is ticked, we get to create a folder called 'Interface', whoses path is written inside the QLineEdit.

With this, if a new folder is already created, and the checkbox is unchecked again, I want to get a message (probably using QMessageBox) to ask the user whether he wants to delete the folder he created at that path or not. If he pressed yes, it would delete the 'Interface' folder. At this point, I only want to delete the folder using the path from the FileSelect function when the checkbox is unticked, but if you could also guide me with the QMessage that would also be helpful.

I'm pretty new at this and I've tried using shutil.rmtree or QDir.removeRecursively inside the bottom function when the check box is setdisabled(true) but the path is on the top function. Unless I'm supposed to do it inside the top function and add the checkbox button as an argument... which i've tried adding a parent and removing self but still doesn't work (probably still wrong). Could you please help and give me some advice!

import os
from os.path import expanduser
import shutil
from PySide2.QtCore import Qt, QFile, QDir
from PySide2.QtWidgets import (QWidget, QLabel, QHBoxLayout, QVBoxLayout, QLineEdit, QCheckBox,
                               QPushButton, QFileDialog)

class MainWidget(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        #Layout
        Frame = QFrame(self)
        Layout = QVBoxLayout(Frame)
        CheckBox = QCheckBox(Frame)
        Layout.addWidget(CheckBox)
        Label = QLabel(Frame)
        Layout.addWidget(Label)
        LineEdit = QLineEdit(Frame)
        Layout.addWidget(LineEdit )
        Button = QPushButton(Frame)
        Layout.addWidget(Button)

        self.CheckBox.stateChanged.connect(self.Enable)
        self.Button.clicked.connect(self.FileSelect)

    def FileSelect(self):
        path = str(QFileDialog.getExistingDirectory(self,"Browse Directory",expanduser("~/Documents/")
                                                    ,QFileDialog.ShowDirsOnly))
        dirName = path + "/Interface"
        self.LineEdit.setText('{}'.format(dirName))
        if path == '':
            self.LineEdit.setText('')
        os.makedirs(dirName, exist_ok=True)

    def Enable(self):
        if self.CheckBox.isChecked():
            self.Label.setDisabled(False)
            self.LineEdit.setDisabled(False)
            self.Button.setDisabled(False)
        else:
            self.Label.setDisabled(True)
            self.LineEdit.setDisabled(True)
            self.Button.setDisabled(True)

Checkbox, button, line edit and button

Thank you in Advance!




Aucun commentaire:

Enregistrer un commentaire