使用Python的tkinter模块实现界面化的批量修改文件名(续)

  之前的《使用Python的tkinte模块实现界面化的批量修改文件名》主要实现了批量移除文件名中的指定字符串,无法进行替换,本文在前面工作的基础上,增加批量替换文件名中指定字符串的功能。
  新增的功能点不多,主要包括:

  • 单选框控件:使用tkinter.Radiobutton函数创建单选框控件,并用value属性设置单选框对应的值,也即选中单选框时得到的值,提前定义好变量(本文中定义了Int变量),创建单选框控件时用variable属性绑定变量。如果要设置默认选中的单选框,则直接设置变量值为指定单选框对应的value值即可;
  • 设置文本框的默认状态:文本框使用state属性设置文本框的可用状态,包括normal/disabled,可以在创建Entry时指定文档框的state属性为disabled,则文本框默认不可用;
  • 修改控件属性:除了在创建控件时指定属性值之外,在程序运行过程中修改控件属性有多种方式(详见参考文献5),本文采用通过字典键设置属性方式动态修改文本框的可用状态。

  批量修改文件名程序的完整代码如所示:

# coding=gbk

import tkinter as tk
import os
from tkinter.filedialog import askdirectory

def BrowseDri():
    txtDirPath.set(askdirectory())

def SetControlStatus():
    mode=processMode.get()
    if(mode==1):
        txtRemoved['state'] = 'normal'
        txtBeforeReplaced['state'] = 'disabled'
        txtAfterReplaced['state'] = 'disabled'
        btnProcess['text']='移除'
    elif mode==2:
        txtRemoved['state'] = 'disabled'
        txtBeforeReplaced['state'] = "normal"
        txtAfterReplaced['state'] = 'normal'
        btnProcess['text']='替换'

def BatchReplaceFileName():
    path = txtDirPath.get()
    mode=processMode.get()

    if(mode==1):
        strOldSign=txtRemovedContent.get()
        strNewSign=""        
    elif mode==2:
        strOldSign=txtBeforeReplactContent.get()
        strNewSign=txtAfterReplactContent.get()

    files=os.listdir(path)
    for onefile in files:
        if onefile.find(strOldSign)<0:
            continue
        oldname=path+"\\"+onefile
        newname=path+"\\"+onefile.replace(strOldSign,strNewSign)
        os.rename(oldname,newname)
        print(oldname,"====>",newname)
        
window=tk.Tk()
window.title('批量处理文件名')
window.geometry('400x300')

tk.Label(window,text='选择文件夹').grid(row=0,column=0)
txtDirPath=tk.StringVar()
tk.Entry(window,textvariable=txtDirPath).grid(row=0,column=1)
tk.Button(window,text='浏览',command=BrowseDri).grid(row=0,column=2)

processMode =tk.IntVar()
tk.Radiobutton(window, text="移除内容", variable=processMode, value=1, command=SetControlStatus).grid(row=1,column=0)
tk.Label(window,text='输入要移除的内容:').grid(row=1,column=1)
txtRemovedContent=tk.StringVar()
txtRemoved=tk.Entry(window,textvariable=txtRemovedContent)
txtRemoved.grid(row=1,column=2)
tk.Radiobutton(window, text="替换内容", variable=processMode, value=2, command=SetControlStatus).grid(row=2,column=0)
tk.Label(window,text='输入替换前的内容:').grid(row=2,column=1)
txtBeforeReplactContent=tk.StringVar()
txtBeforeReplaced=tk.Entry(window,textvariable=txtBeforeReplactContent,state='disabled')
txtBeforeReplaced.grid(row=2,column=2)
tk.Label(window,text='输入替换后的内容:').grid(row=3,column=1)
txtAfterReplactContent=tk.StringVar()
txtAfterReplaced=tk.Entry(window,textvariable=txtAfterReplactContent,state='disabled')
txtAfterReplaced.grid(row=3,column=2)

processMode.set(1)

btnProcess=tk.Button(window,text='移除',command=BatchReplaceFileName)
btnProcess.grid(row=4,column=0)

tk.mainloop()

  最后是程序效果,如下图所示,选择指定文件夹,首先将文件夹中所有文件中的car字符串替换为che@,接着再移除文件名中的@字符。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

参考文献
[1]https://blog.csdn.net/qq_21238607/article/details/108824662
[2]https://blog.csdn.net/flysh13/article/details/123465292
[3]https://blog.csdn.net/cadi2011/article/details/122464311
[4]https://www.runoob.com/python/python-gui-tkinter.html
[5]https://www.cnblogs.com/wbdzt/articles/15516926.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值