Python: tkinter实例改名小工具

  1 #!/usr/bin/env python
  2 #coding=utf-8
  3 # 
  4 # 版权所有 2014 yao_yu (http://blog.csdn.net/yao_yu_126)
  5 # 本代码以MIT许可协议发布
  6 # 文件名批量加.xls后缀
  7 # 2014-04-21  创建
  8 # 
  9 
 10 import os
 11 import tkinter  as tk
 12 from tkinter    import ttk
 13 
 14 
 15 
 16 version = 2014-04-21
 17 app_title = 文件名批量加后缀 Ver: + version
 18 
 19 listdir = os.listdir
 20 isdir = os.path.isdir
 21 isfile = os.path.isfile
 22 path_join = os.path.join
 23 
 24 #----------------------------  Object Visit   ----------------------------#
 25 def visit_directory_files(root, visitor):
 26     for i in listdir(root):
 27         i = path_join(root, i)
 28         if isdir(i):
 29             if visit_directory_files(i, visitor):
 30                 return True
 31         elif isfile(i):
 32             if visitor(i):
 33                 return True
 34 
 35 #----------------------------    Visitor   ----------------------------#
 36 class ListVisitor(object):
 37     def __init__(self, *visitors, terminate = True):
 38         if (visitors 
 39                  and isinstance(visitors, (list, tuple)) 
 40                  and isinstance(visitors[0], (list, tuple))):
 41             visitors = visitors[0]
 42         self._visitors = list(visitors)
 43         self._terminate = terminate
 44     def __call__(self, *args, **kwdargs):
 45         for visitor in self._visitors:
 46             if visitor(*args, **kwdargs):
 47                 return self._terminate
 48     def append(self, visitor):
 49         assert(visitor)
 50         self._visitors.append(visitor)
 51 
 52 def get_screen_size(window):
 53     return window.winfo_screenwidth(),window.winfo_screenheight()
 54   
 55 def get_window_size(window):
 56     return window.winfo_reqwidth(),window.winfo_reqheight()
 57   
 58 def center_window(root, width, height):
 59     screenwidth = root.winfo_screenwidth()
 60     screenheight = root.winfo_screenheight()
 61     size = %dx%d+%d+%d % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
 62     root.geometry(size)
 63     
 64 class Application(object):
 65     def __init__(self, master):
 66         self.master = ttk.Frame(master)
 67         self.master.pack(side = tk.TOP, expand = tk.YES, fill = tk.BOTH)
 68         self.create_widgets()
 69         
 70     def create_widgets(self):
 71         master = self.master
 72         master.columnconfigure(1, weight=1)
 73         master.rowconfigure(2, weight=1)
 74         self.targetdir = tk.StringVar()
 75         self.targetdir.set(/Volumes/Data/Document/Test)
 76         padx = 5
 77         pady = 10
 78         ttk.Label(master, text="操作目录").grid(row = 0, column = 0, stick = tk.E, padx = padx, pady = pady)
 79         ttk.Entry(master, textvariable = self.targetdir).grid(row = 0, column = 1, stick = tk.EW, padx = padx)
 80         commands = ttk.Frame(master)
 81         commands.grid(row = 1, column = 0, columnspan = 2)
 82         ttk.Button(commands, text="开始", command = self.onStart).pack(side = tk.LEFT)
 83         ttk.Button(commands, text="退出", command = master.quit).pack(side = tk.LEFT)
 84         
 85         self.status = tk.StringVar()
 86         self.status.set(就绪)
 87         master.rowconfigure(3, minsize = 160)
 88         ttk.Label(master, textvariable = self.status, wraplength=600).grid(row = 3, column = 0, columnspan = 2, padx = padx, stick = tk.NSEW)
 89         self.progress = ttk.Progressbar(master, maximum=100, orient=tk.HORIZONTAL, mode=determinate)
 90         self.progress.grid(row = 4, column = 0, columnspan = 2, padx = padx, stick = tk.NSEW)
 91         
 92     def onStart(self):
 93         targetdir = self.targetdir.get().strip()
 94         basename = os.path.basename(targetdir)
 95         if os.path.isdir(targetdir):
 96             listvisitor = ListVisitor(ProgressVisitor(self.progress),
 97                                         self.StatusVisitor(),
 98                                         FileLogVisitor(basename),
 99                                         #FileRenameVisitor(basename),
100                                         )
101             visit_directory_files(targetdir, listvisitor)
102         else:
103             self.status.set(目标目录不存在)
104     
105     def StatusVisitor(self):
106         print_status = self.status.set
107         def __call__(file):
108             __call__.n += 1
109             print_status(%s,%s % (__call__.n, file))
110         __call__.n = 0
111         return __call__
112 
113 splitext = os.path.splitext
114 file_rename = os.rename
115 knownexts = dict.fromkeys([.jpg, .log, .pdf, .tif, .xls, .zip, .rar])
116 class FileRenameVisitor(object):
117     def __init__(self, file):
118         self.__fp = open(%s_%s_rename.txt % (os.path.splitext(__file__)[0], file), w)
119     def __call__(self, file):
120         ext = splitext(file)[1].lower()
121         if ext not in knownexts:
122             file_rename(file, file + .xls)
123             self.__fp.write(%s\n % file)
124     def __del__(self):
125         self.__fp.close()
126 
127 class FileLogVisitor(object):
128     def __init__(self, file):
129         self.__fp = open(%s_%s_all.txt % (os.path.splitext(__file__)[0], file), w)
130     def __call__(self, file):
131         self.__fp.write(%s\n % file)
132     def __del__(self):
133         self.__fp.close()
134 
135 class ProgressVisitor(object):
136     COUNT = 202
137     def __init__(self, progress, count=COUNT):
138         self.progress = progress
139         if count and isinstance(count, int) and count > 0:
140             self.count = count
141         else:
142             self.count = self.COUNT
143         self.n = 1
144     def __call__(self, *args, **kwdargs):
145         self.n += 1
146         if self.n == self.count:
147             self.progress.step()
148             self.progress.update()
149             self.n = 1
150     def __del__(self):
151         self.progress[value] = 0
152         
153 
154 if __name__ == __main__:
155     root = tk.Tk()
156     root.title(app_title)
157     app = Application(root)
158     center_window(root, 600, 240)
159     tk.mainloop()

 

Python: tkinter实例改名小工具,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。