This was previously implemented based on functions:Make a folder organizer in Python. This time, the gadget is reimplemented based on an object-oriented approach.
The FileSystem, mentioned in this article, comes from here:Python Organizes Folders Based on File Suffixes
goal
Our aim is to make a tool like the one below, with two input boxes in front for the source path and the target on, images, videos, music below to indicate the folders created in the target path, followed by the file suffixes to indicate that such files will be moved to the corresponding folders, and the plus and minus signs to add or delete folders.
Folder dialog box
The first step is to realize the input and output of the source and target paths, thus requiring a custom component with the following code
import tkinter as tk import as ttk from import (askopenfilename, askopenfilenames, askdirectory, asksaveasfilename) from import askcolor class DialogButton(): def __init__(self, master, height, widthL, widthR, logtype, label=None, text=None, frmDct={}, btnDct={}, enyDct={}, logDct={}): w = widthL + widthR super().__init__(master, height=height, width = w, **frmDct) (fill=) = () if not text else text (self, width=widthL, textvariable=, **enyDct).pack(side=, fill = , expand=True) (self, width=widthR, text=(logtype, label), command = , **btnDct).pack(side=, padx=5) = logtype = logDct def setLabel(self, key, label=None): if label: return label labelDct = { "Documentation" : "Selection of documents", "Folders" : "Choose a path.", "Multiple documents" : "Select multiple files", "Save." : "Storage path", "Color." : "Select Color", } return labelDct[key] def Click(self): typeDct = { "Documentation" : askopenfilename, "Folders": askdirectory, "Multiple documents": askopenfilenames, "Save." : asksaveasfilename, "Color." : askcolor, } text = typeDct[](**) if == "Color.": text = text[1] (text) def get(self): return () def set(self, txt): (txt)
File Mapping Component
In the file mapping list, the UI elements of each row are identical, so a new file mapping component can be created. The file mapping component, mainly consists of two parts, left and right, with the folder name on the left side and the corresponding file suffix on the right side. From these two sub-components, you can set the initialization parameters and methods for them. Let's look at the source code first, and then analyze it paragraph by paragraph
class ExFolder(): def __init__(self, master, folder=None, ex=None, **options): super().__init__(master, **options) () = () if folder : (folder) = () if ex : (ex) () def initWidgets(self): (self, textvariable=, width=10).pack(side=, padx=2) (self, textvariable= ).pack(side=, fill=, expand=True) def getFolder(self): return () def setFolder(self, f): (f) def getEx(self): exs = ().split(",") return [() for ex in exs] def setEx(self, exs): if type(exs)==list: exs = ', '.join([() for ex in exs]) (exs)
First of all, folder and ex are the variable text corresponding to the left and right components, respectively. In order to facilitate updating and calling, the class is designed with two sets and four pairs of portable methods getFolder, setFolder and getEx and setEx. Since the content in folder is the folder itself, the set and read methods are just a secondary StringVar encapsulation, while the contents of the ex is not, you need to split the text, and the realization of the string to list conversion.
complete assembly
Finally, there is the layout of the entire component, with the following source code
class FolderSplit(): def __init__(self, master, **options): super().__init__(master, **options) () = FileSystem() () def initWidgets(self): = [] pDct = dict(side=, expand=True, fill=) = DialogButton(self, 5, 25, 8, "Folders", label="Source path") (**pDct) = DialogButton(self, 5, 25, 8, "Folders", label="Target path") (**pDct) btns = (self) (**pDct) (btns, text="➕ ", command=).grid(row=0,column=0) (btns, text="➖", command=).grid(row=0,column=1) (btns, text="Move.", command=).grid(row=0,column=2) (btns, text="Withdrawn.", command=).grid(row=0,column=3) = (self, text="Folder mapping table") (**pDct) for k, v in (): (, k, v) def btnAddFolder(self): (, None, None) def addFolders(self, frm, folder, exs): ef = ExFolder(frm, folder, exs) (side=, expand=True, fill=, padx=2, pady=1) (ef) def btnDelFolder(self): if len() == 0: return [-1].pack_forget() del [-1] def setExDct(self): fileDct = {() : () for ef in } (fileDct) def btnMove(self): src = () dst = () () if dst=="": dst = src (src, dst) def btnReMove(self): ()
The three implemented function buttons, ➕ is used to add a new folder mapping component and ➖ removes the last mapping component. When removing a component, it is important to note that not only should the component be unbundled, but also the corresponding instance of the component should be deleted.
The core functionality is bound to the move button, which first reads the source and target paths, and works under the source path if the target path is not entered.
Next, the file mapping component is re-disassembled into a mapping dictionary, and finally the move method in FileSystem is called to realize the movement of all files.
to this article on Python based on object-oriented to do a folder organizer to this article, more related Python folder organizer tool content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!