简体   繁体   中英

how to create a folder according to calendar widgets pyqt5?

在此处输入图片说明

I'm made this UI using pyqt5 designer .

Is it possible that when a day(example:22-6-2021) is selected in calendar all files in folder '22-6-2021' shown in a list as in the image and if no files are found a folder is created with some date format ?

The process is:

  • Get the QDate selected in the QCalendarWidget and convert it to a string using a certain format.
  • Using the previous string, do the search in the main directory, and if it does not exist then create the directory.
  • Use the directory that matches the format to set as rootIndex of the QFileSystemModel of the QListView.
import os
import sys
from pathlib import Path


from PyQt5.QtCore import QDir
from PyQt5.QtWidgets import (
    QApplication,
    QCalendarWidget,
    QFileSystemModel,
    QListView,
    QVBoxLayout,
    QWidget,
)

CURRENT_DIRECTORY = Path(__file__).resolve().parent


class Widget(QWidget):
    def __init__(self, root_directory, date_format="dd-M-yyyy", parent=None):
        super().__init__(parent)
        self._root_directory = root_directory
        self._date_format = date_format

        self.calendar_widget = QCalendarWidget()
        self.list_view = QListView()

        lay = QVBoxLayout(self)
        lay.addWidget(self.calendar_widget)
        lay.addWidget(self.list_view)

        self.model = QFileSystemModel()
        self.model.setRootPath(os.fspath(self.root_directory))

        self.calendar_widget.selectionChanged.connect(self.handle_selection_changed)

    @property
    def root_directory(self):
        return self._root_directory

    @property
    def date_format(self):
        return self._date_format

    def handle_selection_changed(self):
        if self.list_view.model() is None:
            self.list_view.setModel(self.model)
        dt = self.calendar_widget.selectedDate()
        dt_str = dt.toString(self.date_format)
        folder = os.fspath(self.root_directory / dt_str)
        if not QDir(folder).exists():
            QDir().mkdir(folder)
        self.list_view.setRootIndex(self.model.index(folder))


def main():
    app = QApplication(sys.argv)
    widget = Widget(CURRENT_DIRECTORY)
    widget.show()
    widget.resize(640, 480)
    widget.show()
    app.exec_()


if __name__ == "__main__":
    main()

maybe i can give you the main idea.. you can make an event listener for the calendar which will create folders

....
self.calendar.selectionChanged.connect(self.calendarListener)

def checkFolders(self, date):
    directory = os.getcwd()
    ls = os.listdir(directory)

    for f in ls:
        if f != date:
            new_dir = os.path.join(directory, date)
            os.mkdir(new_dir)

def calendarListener(self):
    qdate = self.calendar.selectedDate()
    y = qdate.year()
    m = qdate.month()
    d = qdate.day()
    date = str(d) + "-" + str(m) + "-" + str(y)
    print(date)
    self.checkFolders(date)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM