使用GUI--tkinter 制作一个批量修改文件名的桌面软件

'''
title:批量修改文件名称
author:huyang
createtime:2021-01-29 14:50:00
'''
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from pathlib import Path
import os

root = Tk()
root.title('批量修改文件名称-huyang')
root.geometry('300x100')


def select_dir():
    filePath = filedialog.askdirectory()
    label.configure(text=filePath)
    button1['text'] = '开始'

def update_file_name():
    button1['text'] = '修改中...'

    dirPath = label['text'] # 文件夹名字
    imageName = t.get()     # 图片前缀
    filenames = sorted(os.listdir(dirPath))
    file_length = len(filenames)  # 图片数
    os.chdir(dirPath)
    n = 0
    for i in filenames:
        p = Path(i)
        # 判断是否是文件
        if p.is_file:
            n += 1
            new_name = '{name}-{num:0>{num_length}}{houzui}'.format(name=imageName, num=n, num_length=len(str(file_length)), houzui=p.suffix)
            os.rename(i, new_name)

    button1['text'] = '修改完毕'
    messagebox.showinfo(title='信息提示', message='修改成功!')
    

# 第一行
Button(root, text='选择文件夹', command=select_dir).grid(row=0, column=0)
label = Label(root, text=' ', bg='white', width='20', anchor='e')
label.grid(row=0, column=1)


# 第二行
Label(root, text='图片名称前缀:').grid(row=1, column=0)
t = Entry(root)
t.grid(row=1, column=1)

# 第三行
button1 = Button(root, text='开始', command=update_file_name)
button1.grid(row=2, columnspan=2)

root.mainloop()

原文地址:https://www.cnblogs.com/shiyixirui/p/14347663.html