Python is based on the tkinter module to implement the renaming tool example, pythontkinter
This article describes Python's renaming tool based on the tkinter module. We will share this with you for your reference. The details are as follows:
#! /Usr/bin/env python # coding = UTF-8 # copyright 2014 yao_yu # This code is published using the MIT License Agreement # file name approval plus. xls suffix # creation # import osimport tkinter as tkfrom tkinter import ttkversion = '2017-04-21 'app _ title =' Add the suffix Ver to multiple file names: '+ versionlistdir = OS. listdirisdir = OS. path. isdirisfile = OS. path. isfilepath_join = OS. path. join # jsonobject Visit -------------------------- # def visit_directory_files (root, visitor): for I in listdir (root): I = path_join (root, I) if isdir (I ): if visit_directory_files (I, visitor): return True elif isfile (I): if visitor (I): return True # -------------------------- Visitor ---------------------------- # class ListVisitor (object ): def _ init _ (self, * visitors, terminate = True): if (visitors and isinstance (visitors, (list, tuple) and isinstance (visitors [0], (list, tuple): visitors = visitors [0] self. _ visitors = list (visitors) self. _ terminate = terminate def _ call _ (self, * args, ** kwdargs): for visitor in self. _ visitors: if visitor (* args, ** kwdargs): return self. _ terminate def append (self, visitor): assert (visitor) self. _ visitors. append (visitor) def get_screen_size (window): return window. winfo_screenwidth (), window. winfo_screenheight () def get_window_size (window): return window. winfo_reqwidth (), window. winfo_reqheight () def center_window (root, width, height): screenwidth = root. winfo_screenwidth () screenheight = root. winfo_screenheight () size = '% dx % d + % d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2) root. geometry (size) class Application (object): def _ init _ (self, master): self. master = ttk. frame (master) self. master. pack (side = tk. TOP, expand = tk. YES, fill = tk. BOTH) self. create_widgets () def create_widgets (self): master = self. master. columnconfigure (1, weight = 1) master. rowconfigure (2, weight = 1) self.tar getdir = tk. stringVar () self.tar getdir. set ('/Volumes/Data/Document/test') padx = 5 pady = 10 ttk. label (master, text = "Operation directory "). grid (row = 0, column = 0, stick = tk. e, padx = padx, pady = pady) ttk. entry (master, textvariable = self.tar getdir ). grid (row = 0, column = 1, stick = tk. EW, padx = padx) commands = ttk. frame (master) commands. grid (row = 1, column = 0, columnspan = 2) ttk. button (commands, text = "start", command = self. onStart ). pack (side = tk. LEFT) ttk. button (commands, text = "exit", command = master. quit ). pack (side = tk. LEFT) self. status = tk. stringVar () self. status. set ('ready') master. rowconfigure (3, minsize = 160) ttk. label (master, textvariable = self. status, wraplength = 600 ). grid (row = 3, column = 0, columnspan = 2, padx = padx, stick = tk. NSEW) self. progress = ttk. progressbar (master, maximum = 100, orient = tk. HORIZONTAL, mode = 'terminate') self. progress. grid (row = 4, column = 0, columnspan = 2, padx = padx, stick = tk. NSEW) def onStart (self): targetdir = self.tar getdir. get (). strip () basename = OS. path. basename (targetdir) if OS. path. isdir (targetdir): listvisitor = ListVisitor (ProgressVisitor (self. progress), self. statusVisitor (), FileLogVisitor (basename), # FileRenameVisitor (basename),) visit_directory_files (targetdir, listvisitor) else: self. status. set ('destination directory does not exist') def StatusVisitor (self): print_status = self. status. set def _ call _ (file): _ call __. n + = 1 print_status ('% s, % s' % (_ call __. n, file) _ call __. n = 0 return _ call _ splitext = OS. path. splitextfile_rename = OS. renameknownexts = dict.fromkeys('.jpg ','. log', 'hangzhou ','. tif ', '.xls', '.zip ', '.rar']) class FileRenameVisitor (object): def _ init _ (self, file): self. _ fp = open('%s_%s_rename.txt '% (OS. path. splitext (_ file _) [0], file), 'w') def _ call _ (self, file): ext = splitext (file) [1]. lower () if ext not in knownexts: file_rename (file, file + '.xls ') self. _ fp. write ('% s \ n' % file) def _ del _ (self): self. _ fp. close () class FileLogVisitor (object): def _ init _ (self, file): self. _ fp = open('%s_%s_all.txt '% (OS. path. splitext (_ file _) [0], file), 'w') def _ call _ (self, file): self. _ fp. write ('% s \ n' % file) def _ del _ (self): self. _ fp. close () class ProgressVisitor (object): COUNT = 202 def _ init _ (self, progress, count = COUNT): self. progress = progress if count and isinstance (count, int) and count> 0: self. count = count else: self. count = self. COUNT self. n = 1 def _ call _ (self, * args, ** kwdargs): self. n + = 1 if self. n = self. count: self. progress. step () self. progress. update () self. n = 1 def _ del _ (self): self. progress ['value'] = 0if _ name _ = '_ main _': root = tk. tk () root. title (app_title) app = Application (root) center_window (root, 600,240) tk. mainloop ()