1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| import tkinter as tk from tkinter import ttk from tkinter import messagebox from ToolTip import ToolTip import os import time
class Application(tk.Tk):
def __init__(self): super().__init__() self.title("公众号:ZERO开发") self.iconbitmap('logo.ico') self.geometry("600x370") # 设置固定的窗口大小 self.resizable(False, False) # 禁止调整窗口大小 self.style = ttk.Style(self) self.main_color = self.cget("bg")
self.style.configure("TEntry", padding=6, relief="flat", background="#0078d7", foreground="black", font=("Arial", 12, "bold"))
self.style.configure("TLabel", font=("Arial", 12, "bold"))
self.style.configure("TButton", padding=6, font=("Arial", 12))
self.create_widgets()
def create_widgets(self): self.path_label = ttk.Label(self, text='绝对路径:') self.path_label.grid(row=0, sticky=tk.W, pady=30, padx=20)
self.path = tk.StringVar() self.path_entry = ttk.Entry(self, width=60, textvariable=self.path) self.path_entry.grid(row=0, column=1, sticky=tk.E, pady=5) ToolTip(self.path_entry, "电脑里的目录路径,如 D:\\3code\\6pytorch\pytorch_ai_demo")
self.rename_label = ttk.Label(self, text='修改名称:') self.rename_label.grid(row=1, sticky=tk.W, pady=5, padx=20)
self.rename = tk.StringVar() self.rename_entry = ttk.Entry(self, width=60, textvariable=self.rename) self.rename_entry.grid(row=1, column=1, sticky=tk.E, pady=5)
self.msg_text = tk.Text(self, height=2, width=60, wrap='none') # 添加一个列表框来显示文件名 self.msg_text.grid(row=2, column=1, sticky=tk.W, pady=15) self.msg_text.configure(bd=0, relief="solid", bg=self.main_color) # self.msg_text.insert(tk.END, "这是要显示的文本。")
ttk.Button(self, text='确认修改', command=self.start_program).grid(row=3, column=1, sticky=tk.W, pady=20, padx=120) ttk.Button(self, text='关于作者', command=self.about).grid(row=4, column=1, sticky=tk.W, padx=120)
def start_program(self): print("绝对路径:{}, 重命名:{}". format(self.path.get(), self.rename.get()))
path = self.path.get() rename = self.rename.get()
if path == "" or rename == "": messagebox.showwarning("警告", "输入框不能为空!") return
if os.path.isdir(path) == False: messagebox.showwarning("警告", "绝对路径不正确!") return
i = 0 # '该文件夹下所有的文件(包括文件夹)' FileList = os.listdir(path)
# '遍历所有文件' for files in FileList: oldDirPath = os.path.join(path, files)
self.msg_text.delete(1.0, tk.END) self.msg_text.insert(tk.END, oldDirPath)
# '如果是文件夹则递归调用' if os.path.isdir(oldDirPath): self.start_program(oldDirPath)
# '文件名' fileName = os.path.splitext(files)[0] # '文件扩展名' fileType = os.path.splitext(files)[1] fileType = fileType.lower()
newDirPath = os.path.join(path, rename + "_" + str(i) + fileType)
# '重命名' os.rename(oldDirPath, newDirPath) i += 1
messagebox.showinfo("信息", "操作完成!")
def about(self): messagebox.showinfo("关于", "微信公众号:ZERO开发\r\n\r\n工具:批量修改文件1.0")
def quit_program(self): self.destroy()
if __name__ == "__main__": app = Application() app.mainloop()
|