最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Python的tkinter模塊實(shí)現(xiàn)界面化的批量修改文件名

 更新時(shí)間:2022年08月24日 15:37:17   作者:gc_2299  
這篇文章主要介紹了利用Python的tkinter模塊實(shí)現(xiàn)界面化的批量修改文件名,用Python編寫過批量修改文件名的腳本程序,代碼很簡(jiǎn)單,運(yùn)行也比較快,詳細(xì)內(nèi)容需要的小伙伴可以參考一下下面文章內(nèi)容

用Python編寫過批量修改文件名的腳本程序,代碼很簡(jiǎn)單,運(yùn)行也比較快,唯一美中不足之處是每次批量修改文件名時(shí)都需要執(zhí)行以下步驟:

  • 1)復(fù)制文件夾路徑;
  • 2)打開腳本程序
  • 3)替換腳本中的文件夾路徑
  • 4)保存腳本程序
  • 5)執(zhí)行腳本程序

為了便于操作,最好還是弄成GUI界面,手動(dòng)選擇文件夾,這樣程序也更通用。Python中的GUI庫(kù)很多,絕大部分都支持跨平臺(tái),其中安裝python時(shí)自帶的GUI庫(kù)是tkinter,本文就學(xué)習(xí)并創(chuàng)建基于tkinte的批量修改文件名程序。

本文涉及的知識(shí)點(diǎn)包括以下幾個(gè):

  • 1)使用tkinter.Tk創(chuàng)建窗口,并且調(diào)用geometry函數(shù)設(shè)置窗口的寬和高,需要注意的是geometry函數(shù)接收的是字符串,寬度x高度,中間的乘號(hào)其實(shí)是小寫的x,用數(shù)學(xué)符號(hào)或者是大寫的X會(huì)報(bào)錯(cuò);
  • 2)布局方式:根據(jù)參考文獻(xiàn)1,tkinter有pack、grid、place三種布局方式,本文采購(gòu)grid布局方式,該方式有點(diǎn)類似Winfom中的TableLayoutPanel,不同之處在于不用提前設(shè)置好行數(shù)和列數(shù),只需指定所用控件的行號(hào)和列號(hào)即可;
  • 3)控件,主要使用了標(biāo)簽(tkinter.Label)、文本(tkinter.Entry)、按鈕(tkinter.Button)控件,tkinte通過變量綁定的方式獲取或更新文本控件的值。
  • 4)瀏覽文件夾,根據(jù)參考文獻(xiàn)2,調(diào)用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='輸入要移除的內(nèi)容:').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()

最后是程序運(yùn)行效果,如下面幾張截圖所示:運(yùn)行程序后,首先選擇要批量處理的文件夾,然后設(shè)置文件名中要移除的內(nèi)容,最后點(diǎn)擊移除按鈕批量處理文件名。

上文主要實(shí)現(xiàn)了批量移除文件名中的指定字符串,無法進(jìn)行替換,本文在前面工作的基礎(chǔ)上,增加批量替換文件名中指定字符串的功能。

新增的功能點(diǎn)不多,主要包括:

  • 單選框控件:使用tkinter.Radiobutton函數(shù)創(chuàng)建單選框控件,并用value屬性設(shè)置單選框?qū)?yīng)的值,也即選中單選框時(shí)得到的值,提前定義好變量(本文中定義了Int變量),創(chuàng)建單選框控件時(shí)用variable屬性綁定變量。如果要設(shè)置默認(rèn)選中的單選框,則直接設(shè)置變量值為指定單選框?qū)?yīng)的value值即可;
  • 設(shè)置文本框的默認(rèn)狀態(tài):文本框使用state屬性設(shè)置文本框的可用狀態(tài),包括normal/disabled,可以在創(chuàng)建Entry時(shí)指定文檔框的state屬性為disabled,則文本框默認(rèn)不可用;
  • 修改控件屬性:除了在創(chuàng)建控件時(shí)指定屬性值之外,在程序運(yùn)行過程中修改控件屬性有多種方式(詳見參考文獻(xiàn)5),本文采用通過字典鍵設(shè)置屬性方式動(dòng)態(tài)修改文本框的可用狀態(tài)。

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

# 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="移除內(nèi)容", variable=processMode, value=1, command=SetControlStatus).grid(row=1,column=0)
tk.Label(window,text='輸入要移除的內(nèi)容:').grid(row=1,column=1)
txtRemovedContent=tk.StringVar()
txtRemoved=tk.Entry(window,textvariable=txtRemovedContent)
txtRemoved.grid(row=1,column=2)
tk.Radiobutton(window, text="替換內(nèi)容", variable=processMode, value=2, command=SetControlStatus).grid(row=2,column=0)
tk.Label(window,text='輸入替換前的內(nèi)容:').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='輸入替換后的內(nèi)容:').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@,接著再移除文件名中的@字符。

到此這篇關(guān)于利用Python的tkinter模塊實(shí)現(xiàn)界面化的批量修改文件名的文章就介紹到這了,更多相關(guān)Python tkinter批量修改文件名內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

湘潭县| 广河县| 乌兰浩特市| 清苑县| 和林格尔县| 灵寿县| 东兰县| 石柱| 烟台市| 天柱县| 都昌县| 卓尼县| 辽中县| 诸暨市| 枝江市| 安塞县| SHOW| 陵川县| 青浦区| 瑞昌市| 巴东县| 新乡市| 福建省| 东台市| 乌拉特中旗| 宣化县| 舟曲县| 清镇市| 峨眉山市| 瑞丽市| 阳春市| 建昌县| 巴楚县| 永兴县| 大同市| 岳池县| 德安县| 马龙县| 东阿县| 绥江县| 根河市|