Python基于tkinter模塊實(shí)現(xiàn)的改名小工具示例
本文實(shí)例講述了Python基于tkinter模塊實(shí)現(xiàn)的改名小工具。分享給大家供大家參考,具體如下:
#!/usr/bin/env python #coding=utf-8 # # 版權(quán)所有 2014 yao_yu # 本代碼以MIT許可協(xié)議發(fā)布 # 文件名批量加.xls后綴 # 2014-04-21 創(chuàng)建 # import os import tkinter as tk from tkinter import ttk version = '2014-04-21' app_title = '文件名批量加后綴 Ver:' + version listdir = os.listdir isdir = os.path.isdir isfile = os.path.isfile path_join = os.path.join #---------------------------- Object 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+%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 master.columnconfigure(1, weight=1) master.rowconfigure(2, weight=1) self.targetdir = tk.StringVar() self.targetdir.set('/Volumes/Data/Document/Test') padx = 5 pady = 10 ttk.Label(master, text="操作目錄").grid(row = 0, column = 0, stick = tk.E, padx = padx, pady = pady) ttk.Entry(master, textvariable = self.targetdir).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="開(kāi)始", command = self.onStart).pack(side = tk.LEFT) ttk.Button(commands, text="退出", command = master.quit).pack(side = tk.LEFT) self.status = tk.StringVar() self.status.set('就緒') 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='determinate') self.progress.grid(row = 4, column = 0, columnspan = 2, padx = padx, stick = tk.NSEW) def onStart(self): targetdir = self.targetdir.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('目標(biāo)目錄不存在') 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.splitext file_rename = os.rename knownexts = dict.fromkeys(['.jpg', '.log', '.pdf', '.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'] = 0 if __name__ == '__main__': root = tk.Tk() root.title(app_title) app = Application(root) center_window(root, 600, 240) tk.mainloop()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- 利用Tkinter(python3.6)實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器
- python3.6 +tkinter GUI編程 實(shí)現(xiàn)界面化的文本處理工具(推薦)
- python 3.6 tkinter+urllib+json實(shí)現(xiàn)火車車次信息查詢功能
- Python編程使用tkinter模塊實(shí)現(xiàn)計(jì)算器軟件完整代碼示例
- Python tkinter模塊中類繼承的三種方式分析
- Python tkinter模塊彈出窗口及傳值回到主窗口操作詳解
- 基于python的Tkinter編寫(xiě)登陸注冊(cè)界面
- Python+tkinter使用80行代碼實(shí)現(xiàn)一個(gè)計(jì)算器實(shí)例
相關(guān)文章
Python獲取暗黑破壞神3戰(zhàn)網(wǎng)前1000命位玩家的英雄技能統(tǒng)計(jì)
這篇文章主要介紹了Python獲取暗黑3戰(zhàn)網(wǎng)前1000命位玩家的英雄技能統(tǒng)計(jì)的方法,借助urllib2模塊以類似爬蟲(chóng)的機(jī)制來(lái)實(shí)現(xiàn),需要的朋友可以參考下2016-07-07pytorch實(shí)現(xiàn)CNN卷積神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了pytorch實(shí)現(xiàn)CNN卷積神經(jīng)網(wǎng)絡(luò),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02Python使用configparser庫(kù)讀取配置文件
這篇文章主要介紹了Python使用configparser庫(kù)讀取配置文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02關(guān)于pyqt5控件自適應(yīng)窗口超詳細(xì)知識(shí)點(diǎn)匯總
這篇文章主要介紹了關(guān)于pyqt5控件自適應(yīng)窗口超詳細(xì)知識(shí)點(diǎn)匯總,有了布局,再在布局中放置各種控件,我們就能讓控件實(shí)現(xiàn)自適應(yīng)的效果,需要的朋友可以參考下2023-03-03Python機(jī)器學(xué)習(xí)入門(mén)(五)之Python算法審查
這篇文章主要介紹了Python機(jī)器學(xué)習(xí)入門(mén)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08Python疊加兩幅柵格圖像的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Python疊加兩幅柵格圖像的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07