使用python 3 的 tkinter 编写的一个带界面工具
作用:
代码如下:
# File Created: Thursday, 13th June 2019 6:10:56 pm #
# Author: Liu Jing ([email protected]) #
# ----- #
# Last Modified: Friday, 20th December 2019 3:47:42 pm #
# Modified By: Liu Jing ([email protected]) #
# ----- #
# Copyright 1999 - 2019 HT Company #
# 导入tkinter GUI图形库
import tkinter as tk
import tkinter.filedialog as tk_dlg
import os
import shutil # 为了文件的复制
def file_extension(path):
return os.path.splitext(path)[1]
# 从文件对话框中选取指定文件夹
def set_directory(arg):
on_hit = tk_dlg.askdirectory()
arg.set(on_hit)
# 遍历并获取指定的文件
def get_file(src, des):
for root, dirs, files in os.walk(src.get()):
# root 表示当前正在访问的文件夹路径
# dirs 表示该文件夹下的子目录名list
# files 表示该文件夹下的文件list
# 遍历文件
for f in files:
# print(os.path.join(root, f))
# 获取到的文件,利用后缀判断是否需要该文件
types = ('.jpg', '.jpeg', 'png')
if f.endswith(types):
file_name = os.path.join(root, f)
print(file_name)
# copy file_name to des directory
shutil.copy(file_name, des.get())
# 遍历所有的文件夹
# for d in dirs:
# print(os.path.join(root, d))
# 修改指定的文件夹下所有文件名
def modify_files_name(des, prename):
print(prename.get())
for root, dirs, files in os.walk(des.get()):
# root 表示当前正在访问的文件夹路径
# dirs 表示该文件夹下的子目录名list
# files 表示该文件夹下的文件list
# 遍历文件
index = 0
for f in files:
# print(os.path.join(root, f))
# 获取到的文件,利用后缀判断是否需要该文件
types = ('.jpg', '.jpeg', 'png')
if f.endswith(types):
file_name = os.path.join(root, f)
print(file_name)
# modify file name, 文件格式 prename + index + .endswith
new_name = os.path.join(root, prename.get()) + str(index) + file_extension(f)
print(new_name)
os.rename(file_name, new_name)
index += 1
# 创建窗口
window = tk.Tk()
# 设置窗口标题
window.title('DDU-Window')
# 设置窗口大小,这里使用的是小写的x
window.geometry("800x600")
# 创建一个子容器
container = tk.LabelFrame(window, text="获取指定格式的文件", font=24)
container.grid(row=0, column=0, padx=5, pady=5)
label_src = tk.Label(container, text="要查找的目录", font=30, bg='green', width=15)
var_src = tk.StringVar()
label_path_src = tk.Entry(container, textvariable=var_src, font=(30), width=60)
btn_path_src = tk.Button(container, text='设置输入目录', font=(
30), command=lambda: set_directory(var_src))
label_src.grid(row=0, column=0, padx=5, pady=5)
label_path_src.grid(row=0, column=1, padx=5, pady=5)
btn_path_src.grid(row=0, column=2, padx=5, pady=5)
label_des = tk.Label(container, text="要输出的目录", font=30, bg='green', width=15)
var_des = tk.StringVar()
label_path_des = tk.Entry(container, textvariable=var_des, font=(30), width=60)
btn_path_des = tk.Button(container, text='设置输出目录', font=(
30), command=lambda: set_directory(var_des))
label_des.grid(row=1, column=0, padx=5, pady=5)
label_path_des.grid(row=1, column=1, padx=5, pady=5)
btn_path_des.grid(row=1, column=2, padx=5, pady=5)
btn_get_file = tk.Button(container, text='获取指定文件', font=(
30), command=lambda: get_file(var_src, var_des))
btn_get_file.grid(row=2, column=2, padx=5, pady=5)
# 创建一个子容器
container2 = tk.LabelFrame(window, text="修改所有文件名", font=24)
container2.grid(row=1, column=0, padx=5, pady=5)
# 修改文件名
label_modify = tk.Label(container2, text="要修改的文件前缀",
font=30, bg='green', width=15)
var_modify = tk.StringVar()
label_path_modify = tk.Entry(
container2, textvariable=var_modify, font=(30), width=60)
btn_path_modify = tk.Button(container2, text='修改所有文件', font=(
30), command=lambda: modify_files_name(var_des, var_modify))
label_modify.grid(row=3, column=0, padx=5, pady=5)
label_path_modify.grid(row=3, column=1, padx=5, pady=5)
btn_path_modify.grid(row=3, column=2, padx=5, pady=5)
# 进入消息循环, window不断刷新
window.mainloop()
if __name__ == "__main__":
print("begin run " + __file__)