天天看點

使用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@,接着再移除檔案名中的@字元。

使用Python的tkinter子產品實作界面化的批量修改檔案名(續)
使用Python的tkinter子產品實作界面化的批量修改檔案名(續)
使用Python的tkinter子產品實作界面化的批量修改檔案名(續)

參考文獻

[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