利用Python的tkinter模块实现界面化的批量修改文件名

2022-08-24 18:00:25

用Python编写过批量修改文件名的脚本程序,代码很简单,运行也比较快,唯一美中不足之处是每次批量修改文件名时都需要执行以下步骤:

    1)复制文件夹路径;2)打开脚本程序3)替换脚本中的文件夹路径4)保存脚本程序5)执行脚本程序

    为了便于操作,最好还是弄成GUI界面,手动选择文件夹,这样程序也更通用。Python中的GUI库很多,绝大部分都支持跨平台,其中安装python时自带的GUI库是tkinter,本文就学习并创建基于tkinte的批量修改文件名程序。

    本文涉及的知识点包括以下几个:

      1)使用tkinter.Tk创建窗口,并且调用geometry函数设置窗口的宽和高,需要注意的是geometry函数接收的是字符串,宽度x高度,中间的乘号其实是小写的x,用数学符号或者是大写的X会报错;2)布局方式:根据参考文献1,tkinter有pack、grid、place三种布局方式,本文采购grid布局方式,该方式有点类似Winfom中的TableLayoutPanel,不同之处在于不用提前设置好行数和列数,只需指定所用控件的行号和列号即可;3)控件,主要使用了标签(tkinter.Label)、文本(tkinter.Entry)、按钮(tkinter.Button)控件,tkinte通过变量绑定的方式获取或更新文本控件的值。4)浏览文件夹,根据参考文献2,调用tkinter.filedialog中的askdirectory()选择文件夹。

      全部代码如下所示:

      # coding=gbk
      import tkinter as tk
      import os
      from tkinter.filedialog import askdirectory
      def BrowseDri():
          txtDirPath.set(askdirectory())
      
      def BatchReplaceFileName():
          path = txtDirPath.get()
          strSign=txtRemovedContent.get()
          
          files=os.listdir(path)
      
          for onefile in files:
              if onefile.find(strSign)<0:
                  continue
              oldname=path+"\\"+onefile
              newname=path+"\\"+onefile.replace(strSign,"")
              os.rename(oldname,newname)
              print(oldname,"====>",newname)
      
          
      
      window=tk.Tk()
      window.title('批量处理文件名')
      window.geometry('600x400')
      
      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)
      
      tk.Label(window,text='输入要移除的内容:').grid(row=1,column=0)
      txtRemovedContent=tk.StringVar()
      tk.Entry(window,textvariable=txtRemovedContent).grid(row=1,column=1)
      tk.Button(window,text='移除',command=BatchReplaceFileName).grid(row=1,column=2)
      tk.mainloop()

      最后是程序运行效果,如下面几张截图所示:运行程序后,首先选择要批量处理的文件夹,然后设置文件名中要移除的内容,最后点击移除按钮批量处理文件名。

      上文主要实现了批量移除文件名中的指定字符串,无法进行替换,本文在前面工作的基础上,增加批量替换文件名中指定字符串的功能。

      新增的功能点不多,主要包括:

        单选框控件:使用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@,接着再移除文件名中的@字符。

        到此这篇关于利用Python的tkinter模块实现界面化的批量修改文件名的文章就介绍到这了,更多相关Python tkinter批量修改文件名内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!