The process is:
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.