45 鱼币
'''
批量文件/文件夹更名
批量文件/文件夹删除
命名方式:
变量:类缩写小写+名[+后续补充]
属性:分区[+后续分区]_类缩写小写+名[+后续补充]
方法:分区[+后续分区]_返回值的类缩写小写[+1返回值的类缩写小写]+名[+后续补充]
ps:用1间隔有多个可能返回值的类缩写小写 .暂时没想到更好的办法 ,不想额外用_来破坏命名方式的整体感 ,
然后用字母的话会产生拼写错误的检查提醒(下波浪线) ,而这个提醒我可能会需要用到
这样在使用pycharm时 ,查找便方便很多 ,例如:
self.__attrElseBar_barDisplay.func_noneRefreshByCurPath
可以知道它是希望隐藏的(__) ,属性分区的(attr ,) ,其他的bar分区区(ElseBar) ,bar类(bar) ,名为/说明补充为(Display) ,
里的方法区(.func) ,none返回类型(none) ,刷新用当前路径(RefreshByCurPath .)
在self类里面 ,只需self._aebb就能快速索引到self.__attrElseBar_barDisplay
然后因为使用了的是.func ,就可以知道它大概率是需要()的
而例如self.attr_hdHistoryDoing这种 ,其他attr里有Widget的分区 ,只需self.a_就能让Widget分区的属性排在attr_下面,从而方便查找
这命名方式是这里的重点 ,是我自己摸索的(参考了匈牙利命名法) .我冒着被人偷师的风险(因为我最大限度之内写成这样了...)分享出来是希望看能否有更好的名方法值得替换现在这方法 ;或者没准其他人用这命名方法时 ,看他的代码时我能明白的更轻松些
tkinter使用方式:
用grid排版(在横向上比pack方便 ,然后又基本能模拟出pack的所有功能) ,然后用临时Frame来推出padx或pady的效果(直接使用例如padx
,那么在两个使用padx的地方 ,中间间隔就是两个padx的和那么多了 ,而且每多添加一个widget,且使用padx时 ,则需要再次排padx的位置了)
每个widget之间 ,row和column的间隔为10 ,这样可以预览10个共用的位置 ,给tk.Frame()这临时框架用来挤推设置的框架 ,产生排布的效果
代码的写法:
尽量以'先准备 ,然后使用的方式' .例如每个def func的编写情况 .但总有可能遇到像 intFileBaseIndex这类 ,需要运行后才可能知道具体值
的变量 ,暂时没有跟好的办法处理
'''
import os
import re
import tkinter as tk
import tkinter.filedialog as tkf
import send2trash
class HistoryPath:
def __init__(self ,strStart):
'''self.attr_listBack[0] 相当于最初 ,self.attr_listBack[-1]相对与当前'''
self.attr_listBack = [strStart]
self.attr_listFore = []
def func_strGetCurPath(self):
return self.attr_listBack[-1]
def funcDoing_noneEntryPath(self ,strNewPath):
strNewPath = strNewPath
#
self.attr_listBack.append(strNewPath)
self.attr_listFore.clear()
return None
def funcDoing_noneBackPath(self):
strOldPath = self.attr_listBack[-1]
#
del self.attr_listBack[-1]
self.attr_listFore.append(strOldPath)
return None
def funcDoing_noneForePath(self):
strNewPath = self.attr_listFore[-1]
#
self.attr_listBack.append(strNewPath)
del self.attr_listFore[-1]
return None
class HistoryDoing:
def __init__(self):
'''list里的信息为[(索引 ,完整的旧路径 ,完整的新路径 ,旧文件名 ,新文件名) ,....]
self.attr_listBack[0] 相当于最初 ,self.attr_listBack[-1]相对与当前
'''
self.attr_listBack = []
self.attr_listFore = []
def func_list1noneGetLastDoingInfoList(self):
try:
return self.attr_listBack[-1]
except:
print('没有操作记录')
return None
def func_list1noneGetForeListInfo(self):
try:
return self.attr_listFore[-1]
except:
print('没有操作记录')
return None
def funcDoing_noneEntryDoing(self ,listDoingInfo):
listNewDoingInfo = listDoingInfo
self.attr_listBack.append(listNewDoingInfo)
self.attr_listFore.clear()
return None
def funcDoing_noneBackDoing(self):
listOldDoingInfo = self.attr_listBack[-1]
#
del self.attr_listBack[-1]
self.attr_listFore.append(listOldDoingInfo)
return None
def funcDoing_noneForeDoing(self):
listNewDoingInfo = self.attr_listFore[-1]
#
self.attr_listBack.append(listNewDoingInfo)
del self.attr_listFore[-1]
return None
class BarBar_rename:
def __init__(self):
# attr
self.attr_hdHistoryDoing = HistoryDoing()
self.attrWidget_F = tk.Frame(bd=1 ,relief=tk.SUNKEN)
self.attrWidget_lChooseRange = tk.Label(text='选择作用范围:')
self.attr_svChooseRange = tk.StringVar(value='递归至子文件')
self.attrWidget_rbTree = tk.Radiobutton(text='递归至子文件' ,
variable=self.attr_svChooseRange ,
value='递归至子文件')
self.attrWidget_rbOnlyHere = tk.Radiobutton(text='仅当前路径里的文件' ,
variable=self.attr_svChooseRange ,
value='仅当前路径里的文件')
self.attrWidget_lChooseType = tk.Label(text='选择作用类型:')
self.attr_svFolder = tk.StringVar(value='文件夹')
self.attrWidget_cbFolder = tk.Checkbutton(text='文件夹' ,
variable=self.attr_svFolder ,
onvalue='文件夹' ,offvalue='')
self.attr_svFile = tk.StringVar(value='文件')
self.attrWidget_cbFile = tk.Checkbutton(text='文件' ,
variable=self.attr_svFile ,
onvalue='文件' ,offvalue='')
self.attrWidget_lFindElement = tk.Label(text='要查找的(正则)(为空的话则刷新):')
self.attr_svFindElement = tk.StringVar()
self.attrWidget_eFindElement = tk.Entry(textvariable=self.attr_svFindElement)
self.attrWidget_lReplElement = tk.Label(text='要查找的(正则)(enter键可预览):')
self.attr_svReplElement = tk.StringVar()
self.attrWidget_eReplElement = tk.Entry(textvariable=self.attr_svReplElement)
self.attrWidget_bDoing = tk.Button(text='确定更名' ,width=12)
self.attrWidget_bBack = tk.Button(text='撤销' ,width=12)
self.attrWidget_bFore = tk.Button(text='前进' ,width=12)
# self.attrWidget_bOpenHistoryBar = tk.Button(text='打开修改记录' ,width=12)
# bindCommand
self.attrWidget_eFindElement.bind('<FocusOut>' ,self.funcWidget_noneEFindElementBindFocusOutAndReturn)
self.attrWidget_eFindElement.bind('<Return>' ,self.funcWidget_noneEFindElementBindFocusOutAndReturn)
self.attrWidget_eReplElement.bind('<Return>' ,self.funcWidget_noneEReplElementBindReturn)
self.attrWidget_bDoing.config(command=self.funcWidget_noneBDoingCmd)
self.attrWidget_bBack.config(command=self.funcWidget_noneBBackCmd)
self.attrWidget_bFore.config(command=self.funcWidget_noneBForeCmd)
# func
def func_noneForgetFrame(self):
self.attrWidget_F.grid_forget()
return None
def func_noneCheck(self):
if self.attr_hdHistoryDoing.attr_listBack == []:
self.attrWidget_bBack.config(state=tk.DISABLED)
else:
self.attrWidget_bBack.config(state=tk.NORMAL)
if self.attr_hdHistoryDoing.attr_listFore == []:
self.attrWidget_bFore.config(state=tk.DISABLED)
else:
self.attrWidget_bFore.config(state=tk.NORMAL)
return None
# forBar
def funcBar_noneAddElseBar(self ,barTop ,barDisplay):
self.__attrElseBar_barTop = barTop
self.__attrElseBar_barDisplay = barDisplay
return None
def funcBar_noneFirstRun(self):
self.func_noneCheck()
return None
def funcBar_noneGrid(self ,*args ,**kwargs):
self.attrWidget_F.grid(*args ,**kwargs)
self.attrWidget_F.grid_rowconfigure([0] ,weight=0)
self.attrWidget_F.grid_columnconfigure([20] ,weight=1)
#
tk.Frame(height=5).grid(in_=self.attrWidget_F ,row=0 ,column=0)
tk.Frame(height=5).grid(in_=self.attrWidget_F ,row=1000 ,column=0)
tk.Frame(width=5).grid(in_=self.attrWidget_F ,row=0 ,column=0)
tk.Frame(width=7).grid(in_=self.attrWidget_F ,row=0 ,column=1000)
tk.Frame(width=65).grid(in_=self.attrWidget_F ,row=0 ,column=20 - 1)
self.attrWidget_lChooseRange.grid(in_=self.attrWidget_F ,row=10 ,column=10 ,sticky='w' ,columnspan=1 + 10)
self.attrWidget_rbTree.grid(in_=self.attrWidget_F ,row=20 ,column=20 ,sticky='w')
self.attrWidget_rbOnlyHere.grid(in_=self.attrWidget_F ,row=30 ,column=20 ,sticky='w')
self.attrWidget_lChooseType.grid(in_=self.attrWidget_F ,row=40 ,column=10 ,sticky='w' ,columnspan=1 + 10)
self.attrWidget_cbFolder.grid(in_=self.attrWidget_F ,row=50 ,column=20 ,sticky='w')
self.attrWidget_cbFile.grid(in_=self.attrWidget_F ,row=60 ,column=20 ,sticky='w')
self.attrWidget_lFindElement.grid(in_=self.attrWidget_F ,row=70 ,column=10 ,sticky='w' ,columnspan=1 + 10)
self.attrWidget_eFindElement.grid(in_=self.attrWidget_F ,row=80 ,column=20 ,sticky='wesn')
self.attrWidget_lReplElement.grid(in_=self.attrWidget_F ,row=90 ,column=10 ,sticky='w' ,columnspan=1 + 10)
self.attrWidget_eReplElement.grid(in_=self.attrWidget_F ,row=100 ,column=20 ,sticky='wesn')
tk.Frame(height=10).grid(in_=self.attrWidget_F ,row=110 - 1 ,column=0)
self.attrWidget_bDoing.grid(in_=self.attrWidget_F ,row=110 ,column=20 ,sticky='e')
tk.Frame(height=20).grid(in_=self.attrWidget_F ,row=120 - 1 ,column=0)
self.attrWidget_bBack.grid(in_=self.attrWidget_F ,row=120 ,column=20 ,sticky='e')
tk.Frame(height=5).grid(in_=self.attrWidget_F ,row=130 - 1 ,column=0)
self.attrWidget_bFore.grid(in_=self.attrWidget_F ,row=130 ,column=20 ,sticky='e')
return None
# forWidget
def funcWidget_noneEFindElementBindFocusOutAndReturn(self ,evnet=None):
strFindElement = self.attr_svFindElement.get()
if strFindElement == '':
self.__attrElseBar_barDisplay.func_noneRefreshByCurPath()
else:
strCurPath = self.__attrElseBar_barTop.func_strGetCurPath()
strFolder = self.attr_svFolder.get()
strFile = self.attr_svFile.get()
strPattern = self.attr_svFindElement.get()
strRepl = self.attr_svReplElement.get()
#
intFileBaseIndex = 0
#
self.__attrElseBar_barDisplay.func_noneClearSelectionAll()
for strTop ,listFolders ,listFiles in os.walk(strCurPath):
if strFolder == '文件夹':
for intIndex ,strEachFolder in enumerate(listFolders):
strOldElement = strEachFolder
strNewElement = re.sub(strPattern ,strRepl ,strOldElement)
if strNewElement != strOldElement: # 能匹配得到的话
self.__attrElseBar_barDisplay.func_noneSelectionSet(intIndex)
if strFile == '文件':
intFileBaseIndex = len(listFolders)
for intIndex ,strEachFile in enumerate(listFiles):
intFileTrueIndex = intIndex + intFileBaseIndex
strOldElement = strEachFile
strNewElement = re.sub(strPattern ,strRepl ,strOldElement)
if strNewElement != strOldElement: # 能匹配得到的话
self.__attrElseBar_barDisplay.func_noneSelectionSet(intFileTrueIndex)
break
return None
def funcWidget_noneEReplElementBindReturn(self ,event=None):
strCurPath = self.__attrElseBar_barTop.func_strGetCurPath()
strFolder = self.attr_svFolder.get()
strFile = self.attr_svFile.get()
strPattern = self.attr_svFindElement.get()
strRepl = self.attr_svReplElement.get()
#
intFileTrueIndex = 0
#
if bool(strPattern):
self.__attrElseBar_barDisplay.func_noneClearSelectionAll()
for strTop ,listFolders ,listFiles in os.walk(strCurPath):
if strFolder == '文件夹':
for intIndex ,strEachFolder in enumerate(listFolders):
strOldElement = strEachFolder
strNewElement = re.sub(strPattern ,strRepl ,strOldElement)
if strNewElement != strOldElement: # 能匹配得到的话
self.__attrElseBar_barDisplay.func_noneReplaceRowByIndex(intIndex ,strNewElement)
self.__attrElseBar_barDisplay.func_noneSelectionSet(intIndex)
if strFile == '文件':
intFileBaseIndex = len(listFolders)
for intIndex ,strEachFile in enumerate(listFiles):
intFileTrueIndex = intFileBaseIndex + intIndex
strOldElement = strEachFile
strNewElement = re.sub(strPattern ,strRepl ,strOldElement)
if strNewElement != strOldElement: # 能匹配得到的话
self.__attrElseBar_barDisplay.func_noneReplaceRowByIndex(intFileTrueIndex ,strNewElement)
self.__attrElseBar_barDisplay.func_noneSelectionSet(intFileTrueIndex)
break
return None
def funcWidget_noneBDoingCmd(self ,event=None):
strCurPath = self.__attrElseBar_barTop.func_strGetCurPath()
strChooseRange = self.attr_svChooseRange.get()
strFolder = self.attr_svFolder.get()
strFile = self.attr_svFile.get()
strPattern = self.attr_svFindElement.get()
strRepl = self.attr_svReplElement.get()
#
if bool(strPattern):
listDoingInfo = []
for strTop ,listFolders ,listFiles in os.walk(strCurPath):
if strFolder == '文件夹':
for intIndex ,strEachFolder in enumerate(listFolders):
strOldElement = strEachFolder
strNewElement = re.sub(strPattern ,strRepl ,strOldElement)
if strNewElement != strOldElement: # 能匹配得到的话
strOldPath = os.path.abspath(strTop + os.sep + strOldElement)
strNewPath = os.path.abspath(strTop + os.sep + strNewElement)
os.renames(strOldPath ,strNewPath)
listDoingInfo.append((intIndex ,strOldPath ,strNewPath ,strOldElement ,strNewElement))
if strFile == '文件':
intFileBaseIndex = len(listFolders)
for intIndex ,strEachFile in enumerate(listFiles):
intFileTrueIndex = intIndex + intFileBaseIndex
strOldElement = strEachFile
strNewElement = re.sub(strPattern ,strRepl ,strOldElement)
if strNewElement != strOldElement: # 能匹配得到的话
strOldPath = os.path.abspath(strTop + os.sep + strOldElement)
strNewPath = os.path.abspath(strTop + os.sep + strNewElement)
os.renames(strOldPath ,strNewPath)
listDoingInfo.append(
(intFileTrueIndex ,strOldPath ,strNewPath ,strOldElement ,strNewElement))
self.__attrElseBar_barDisplay.func_noneRefreshByCurPath()
if strChooseRange == '仅当前路径里的文件':
break
elif strChooseRange == '递归至子文件':
continue
self.attr_hdHistoryDoing.funcDoing_noneEntryDoing(listDoingInfo)
self.func_noneCheck()
return None
def funcWidget_noneBBackCmd(self ,event=None):
listOldDoingInfo = self.attr_hdHistoryDoing.func_list1noneGetLastDoingInfoList()
self.__attrElseBar_barDisplay.func_noneClearSelectionAll()
for intIndex ,strOldPath ,strNewPath ,strOldFilename ,strNewFilename in listOldDoingInfo:
os.renames(strNewPath ,strOldPath)
self.__attrElseBar_barDisplay.func_noneReplaceRowByIndex(intIndex ,strOldFilename)
self.__attrElseBar_barDisplay.func_noneSelectionSet(intIndex)
self.attr_hdHistoryDoing.funcDoing_noneBackDoing()
self.func_noneCheck()
return None
def funcWidget_noneBForeCmd(self ,event=None):
listForeDoingInfo = self.attr_hdHistoryDoing.func_list1noneGetForeListInfo()
self.__attrElseBar_barDisplay.func_noneClearSelectionAll()
for intIndex ,strOldPath ,strNewPath ,strOldFilename ,strNewFilename in listForeDoingInfo:
os.renames(strOldPath ,strNewPath)
self.__attrElseBar_barDisplay.func_noneReplaceRowByIndex(intIndex ,strNewFilename)
self.__attrElseBar_barDisplay.func_noneSelectionSet(intIndex)
self.attr_hdHistoryDoing.funcDoing_noneForeDoing()
self.func_noneCheck()
return None
class BarBar_delFile:
def __init__(self):
self.attrWidget_F = tk.Frame(bd=1 ,relief=tk.SUNKEN)
self.attrWidget_lChooseRange = tk.Label(text='选择作用范围:')
self.attr_svChooseRange = tk.StringVar(value='递归至子文件')
self.attrWidget_rbTree = tk.Radiobutton(text='递归至子文件' ,
variable=self.attr_svChooseRange ,
value='递归至子文件')
self.attrWidget_rbOnlyHere = tk.Radiobutton(text='仅当前路径里的文件' ,
variable=self.attr_svChooseRange ,
value='仅当前路径里的文件')
self.attrWidget_lChooseType = tk.Label(text='选择作用类型:')
self.attr_svFolder = tk.StringVar(value='')
self.attrWidget_cbFolder = tk.Checkbutton(text='文件夹' ,
variable=self.attr_svFolder ,
onvalue='文件夹' ,offvalue='')
self.attr_svFile = tk.StringVar(value='文件')
self.attrWidget_cbFile = tk.Checkbutton(text='文件' ,
variable=self.attr_svFile ,
onvalue='文件' ,offvalue='')
self.attrWidget_lFindElement = tk.Label(text='要查找的(正则)(依靠元素选中行):')
self.attr_svFindElement = tk.StringVar()
self.attrWidget_eFindElement = tk.Entry(textvariable=self.attr_svFindElement)
self.attrWidget_bDoing = tk.Button(text='确定删除' ,width=12)
# bindCommand
self.attrWidget_eFindElement.bind('<FocusOut>' ,self.funcWidget_noneEFindElementBindFocusOutAndReturn)
self.attrWidget_eFindElement.bind('<Return>' ,self.funcWidget_noneEFindElementBindFocusOutAndReturn)
self.attrWidget_bDoing.config(command=self.funcWidget_noneBDoingCmd)
# func
def func_noneForgetFrame(self):
self.attrWidget_F.grid_forget()
return None
# forBar
def funcBar_noneAddElseBar(self ,barTop ,barDisplay):
self.__attrElseBar_barTop = barTop
self.__attrElseBar_barDisplay = barDisplay
return None
def funcBar_noneFirstRun(self):
return None
def funcBar_noneGrid(self ,*args ,**kwargs):
self.attrWidget_F.grid(*args ,**kwargs)
self.attrWidget_F.grid_rowconfigure([0] ,weight=0)
self.attrWidget_F.grid_columnconfigure([20] ,weight=1)
#
tk.Frame(height=5).grid(in_=self.attrWidget_F ,row=0 ,column=0)
tk.Frame(height=5).grid(in_=self.attrWidget_F ,row=1000 ,column=0)
tk.Frame(width=5).grid(in_=self.attrWidget_F ,row=0 ,column=0)
tk.Frame(width=7).grid(in_=self.attrWidget_F ,row=0 ,column=1000)
tk.Frame(width=65).grid(in_=self.attrWidget_F ,row=0 ,column=20 - 1)
self.attrWidget_lChooseRange.grid(in_=self.attrWidget_F ,row=10 ,column=10 ,sticky='w' ,columnspan=1 + 10)
self.attrWidget_rbTree.grid(in_=self.attrWidget_F ,row=20 ,column=20 ,sticky='w')
self.attrWidget_rbOnlyHere.grid(in_=self.attrWidget_F ,row=30 ,column=20 ,sticky='w')
self.attrWidget_lChooseType.grid(in_=self.attrWidget_F ,row=40 ,column=10 ,sticky='w' ,columnspan=1 + 10)
self.attrWidget_cbFolder.grid(in_=self.attrWidget_F ,row=50 ,column=20 ,sticky='w')
self.attrWidget_cbFile.grid(in_=self.attrWidget_F ,row=60 ,column=20 ,sticky='w')
self.attrWidget_lFindElement.grid(in_=self.attrWidget_F ,row=70 ,column=10 ,sticky='w' ,columnspan=1 + 10)
self.attrWidget_eFindElement.grid(in_=self.attrWidget_F ,row=80 ,column=20 ,sticky='wesn')
tk.Frame(height=10).grid(in_=self.attrWidget_F ,row=90 - 1 ,column=0)
self.attrWidget_bDoing.grid(in_=self.attrWidget_F ,row=90 ,column=20 ,sticky='e')
return None
# forWidget
def funcWidget_noneEFindElementBindFocusOutAndReturn(self ,evnet=None):
strCurPath = self.__attrElseBar_barTop.func_strGetCurPath()
strFolder = self.attr_svFolder.get()
strFile = self.attr_svFile.get()
strPattern = self.attr_svFindElement.get()
#
self.__attrElseBar_barDisplay.func_noneClearSelectionAll()
for strTop ,listFolders ,listFiles in os.walk(strCurPath):
if strFolder == '文件夹':
for intIndex ,strEachFolder in enumerate(listFolders):
strOldElement = strEachFolder
strNewElement = re.search(strPattern ,strOldElement)
if strNewElement: # 能匹配得到的话
self.__attrElseBar_barDisplay.func_noneSelectionSet(intIndex)
if strFile == '文件':
intFileBaseIndex = len(listFolders)
for intIndex ,strFileName in enumerate(listFiles):
intFileTrueIndex = intIndex + intFileBaseIndex
strOldElement = strFileName
matchFlag = re.search(strPattern ,strOldElement)
if matchFlag: # 能匹配得到的话
self.__attrElseBar_barDisplay.func_noneSelectionSet(intFileTrueIndex)
break
return None
def funcWidget_noneBDoingCmd(self ,event=None):
strChooseRange = self.attr_svChooseRange.get()
strCurPath = self.__attrElseBar_barTop.func_strGetCurPath()
strFolder = self.attr_svFolder.get()
strFile = self.attr_svFile.get()
strPattern = self.attr_svFindElement.get()
#
for strTop ,listFolders ,listFiles in os.walk(strCurPath):
if strFolder == '文件夹':
for strFolderName in listFolders:
strOldElement = strFolderName
matchFlag = re.search(strPattern ,strOldElement)
if strNewElement: # 能匹配得到的话
strFullPath = os.path.abspath(strTop + os.sep + strOldElement)
send2trash.send2trash(strFullPath)
if strFile == '文件':
intFileBaseIndex = len(listFolders)
for strFileName in listFiles:
strOldElement = strFileName
matchFlag = re.search(strPattern ,strOldElement)
if matchFlag: # 能匹配得到的话
strFullPath = os.path.abspath(strTop + os.sep + strOldElement)
send2trash.send2trash(strFullPath)
self.__attrElseBar_barDisplay.func_noneRefreshByCurPath()
if strChooseRange == '仅当前路径里的文件':
break
elif strChooseRange == '递归至子文件':
continue
return None
class Bar_top:
def __init__(self):
# attr
self.attr_hpHistoryPath = HistoryPath(os.path.abspath(os.curdir))
# widget
self.attrWidget_F = tk.Frame()
self.attrWidget_bChoosePath = tk.Button(text='选择文件夹' ,width=10)
self.attrWidget_bBackPath = tk.Button(text='返回' ,width=10)
self.attrWidget_bForePath = tk.Button(text='前进' ,width=10)
self.attr_svEntry = tk.StringVar(value=os.path.abspath(os.curdir))
self.attrWidget_eEntryPath = tk.Entry(textvariable=self.attr_svEntry)
# bindCommand
self.attrWidget_bChoosePath.config(command=self.funcWidget_noneBChoosePathCmd)
self.attrWidget_bBackPath.config(command=self.funcWidget_noneBBackPathCmd)
self.attrWidget_bForePath.config(command=self.funcWidget_noneBForePathCmd)
self.attrWidget_eEntryPath.bind('<FocusOut>' ,self.funcWidget_noneEEntryPathBindFocusOutAndReturn)
self.attrWidget_eEntryPath.bind('<Return>' ,self.funcWidget_noneEEntryPathBindFocusOutAndReturn)
# func
# forOther
def func_noneCheckHistory(self):
if self.attr_hpHistoryPath.attr_listBack[0] == self.attr_hpHistoryPath.attr_listBack[-1]:
self.attrWidget_bBackPath.config(state=tk.DISABLED)
else:
self.attrWidget_bBackPath.config(state=tk.NORMAL)
if self.attr_hpHistoryPath.attr_listFore == []:
self.attrWidget_bForePath.config(state=tk.DISABLED)
else:
self.attrWidget_bForePath.config(state=tk.NORMAL)
return None
def func_strGetCurPath(self):
return self.attr_hpHistoryPath.func_strGetCurPath()
def func_noneSetCurPath(self ,strNewPath):
self.attr_hpHistoryPath.funcDoing_noneEntryPath(strNewPath)
self.attr_svEntry.set(strNewPath)
return None
# forMain
def funcMain_noneAddElseBar(self ,barDisplay):
self.__attrElseBar_barDisplay = barDisplay
return None
def funcMain_noneFirstRun(self):
self.func_noneCheckHistory()
return None
def funcMain_noneGrid(self ,*args ,**kwargs):
self.attrWidget_F.grid(*args ,**kwargs)
self.attrWidget_F.grid_rowconfigure([0] ,weight=0)
self.attrWidget_F.grid_columnconfigure([40] ,weight=1)
# eachWidgetGrid
tk.Frame(width=5).grid(in_=self.attrWidget_F ,row=0 ,column=10 - 1)
self.attrWidget_bChoosePath.grid(in_=self.attrWidget_F ,row=10 ,column=10)
tk.Frame(width=5).grid(in_=self.attrWidget_F ,row=0 ,column=10 + 1)
self.attrWidget_bBackPath.grid(in_=self.attrWidget_F ,row=10 ,column=20)
tk.Frame(width=5).grid(in_=self.attrWidget_F ,row=0 ,column=20 + 1)
self.attrWidget_bForePath.grid(in_=self.attrWidget_F ,row=10 ,column=30)
tk.Frame(width=5).grid(in_=self.attrWidget_F ,row=0 ,column=30 + 1)
self.attrWidget_eEntryPath.grid(in_=self.attrWidget_F ,row=10 ,column=40 ,sticky='wesn')
tk.Frame(width=5).grid(in_=self.attrWidget_F ,row=0 ,column=40 + 1)
return None
# forSelfWidget
def funcWidget_noneBChoosePathCmd(self ,event=None):
strChoosePath = tkf.askdirectory()
if strChoosePath == '': # 过滤'取消'
return None
strNewPath = os.path.abspath(strChoosePath)
strOldPath = self.attr_hpHistoryPath.func_strGetCurPath()
if strNewPath != strOldPath: # 过滤'与原路径相同'的情况
self.attr_hpHistoryPath.funcDoing_noneEntryPath(strNewPath)
self.attr_svEntry.set(strNewPath)
self.func_noneCheckHistory()
self.__attrElseBar_barDisplay.func_noneRefreshByCurPath()
return None
def funcWidget_noneBBackPathCmd(self ,event=None):
strNewPath = self.attr_hpHistoryPath.attr_listBack[-2]
# strOldPath = self.attr_hpHistoryPath.attr_listBack[-1]
#
self.attr_hpHistoryPath.funcDoing_noneBackPath()
self.attr_svEntry.set(strNewPath)
self.func_noneCheckHistory()
self.__attrElseBar_barDisplay.func_noneRefreshByCurPath()
return None
def funcWidget_noneBForePathCmd(self ,event=None):
strNewPath = self.attr_hpHistoryPath.attr_listFore[-1]
# strOldPath = self.attr_hpHistoryPath.attr_listFore[-1]
#
self.attr_hpHistoryPath.funcDoing_noneForePath()
self.attr_svEntry.set(strNewPath)
self.func_noneCheckHistory()
self.__attrElseBar_barDisplay.func_noneRefreshByCurPath()
return None
def funcWidget_noneEEntryPathBindFocusOutAndReturn(self ,event=None):
strNewPath = os.path.abspath(self.attrWidget_eEntryPath.get())
if strNewPath == '..':
strNewPath = os.path.abspath(self.attr_hpHistoryPath.func_strGetCurPath() + os.sep + '..')
strOldPath = self.attr_hpHistoryPath.func_strGetCurPath()
if os.path.isdir(strNewPath) and strNewPath != strOldPath: # 过滤'错误的路径'And'原来的路径'
self.attr_hpHistoryPath.funcDoing_noneEntryPath(strNewPath)
self.attr_svEntry.set(strNewPath)
self.func_noneCheckHistory()
self.__attrElseBar_barDisplay.func_noneRefreshByCurPath()
return None
class Bar_control:
def __init__(self):
self.attrWidget_F = tk.Frame()
self.attr_svMode = tk.StringVar(value='批量重命名')
self.attrWidget_rbRename = tk.Radiobutton(text='批量重命名' ,width=15 ,indicatoron=False ,
variable=self.attr_svMode ,
value='批量重命名' ,
command=None)
self.attrWidget_rbDelFile = tk.Radiobutton(text='批量删除' ,width=15 ,indicatoron=False ,
variable=self.attr_svMode ,
value='批量删除' ,
command=None)
# bindcommand
self.attrWidget_rbRename.config(command=self.funcWidget_noneRbRenameCmd)
self.attrWidget_rbDelFile.config(command=self.funcWidget_noneRbDelFileCmd)
# func
# forMain
def funcMain_noneAddElseBar(self ,barTop ,barDisplay):
self.__attrElseBarTop = barTop
self.__attrElseBarDisplay = barDisplay
self.__attr_barRename = BarBar_rename()
self.__attr_barDelFile = BarBar_delFile()
self.__attr_barRename.funcBar_noneAddElseBar(self.__attrElseBarTop ,self.__attrElseBarDisplay)
self.__attr_barDelFile.funcBar_noneAddElseBar(self.__attrElseBarTop ,self.__attrElseBarDisplay)
self.__attr_barRename.funcBar_noneFirstRun()
self.__attr_barRename.funcBar_noneFirstRun()
return None
def funcMain_noneFirstRun(self):
self.funcWidget_noneRbRenameCmd()
return None
def funcMain_noneGrid(self ,*args ,**kwargs):
self.attrWidget_F.grid(*args ,**kwargs)
self.attrWidget_F.grid_rowconfigure([0] ,weight=0)
self.attrWidget_F.grid_columnconfigure([0] ,weight=0)
# eachWidgetGrid
# minWidthAndHeight
#
tk.Frame(height=10).grid(in_=self.attrWidget_F ,row=0 ,column=0)
tk.Frame(height=5).grid(in_=self.attrWidget_F ,row=1000 ,column=0)
tk.Frame(width=5).grid(in_=self.attrWidget_F ,row=0 ,column=0)
tk.Frame(width=5).grid(in_=self.attrWidget_F ,row=0 ,column=1000)
#
self.attrWidget_rbRename.grid(in_=self.attrWidget_F ,row=10 ,column=10 ,sticky='wn')
tk.Frame(width=5).grid(in_=self.attrWidget_F ,row=0 ,column=10 + 1)
self.attrWidget_rbDelFile.grid(in_=self.attrWidget_F ,row=10 ,column=20 ,sticky='wn')
tk.Frame(height=5).grid(in_=self.attrWidget_F ,row=10 + 1 ,column=0)
return None
# forWidget
def funcWidget_noneRbRenameCmd(self ,event=None):
self.__attr_barDelFile.func_noneForgetFrame()
self.__attr_barRename.funcWidget_noneEFindElementBindFocusOutAndReturn()
self.__attr_barRename.funcBar_noneGrid(in_=self.attrWidget_F ,row=20 ,column=10 ,sticky='wesn' ,
columnspan=1000 - 10 - 1)
return None
def funcWidget_noneRbDelFileCmd(self ,event=None):
self.__attr_barRename.func_noneForgetFrame()
self.__attrElseBarDisplay.func_noneRefreshByCurPath()
self.__attr_barDelFile.funcBar_noneGrid(in_=self.attrWidget_F ,row=20 ,column=10 ,sticky='wesn' ,
columnspan=1000 - 10 - 1)
return None
class Bar_display:
def __init__(self):
self.attrWidget_F = tk.Frame()
self.attrWidget_lDisplay = tk.Listbox(selectmode=tk.EXTENDED)
self.attrWidget_ysDisplay = tk.Scrollbar()
self.attrWidget_xsDisplay = tk.Scrollbar(orient='h')
#
self.attrWidget_lDisplay.config(yscrollcommand=self.attrWidget_ysDisplay.set ,
xscrollcommand=self.attrWidget_xsDisplay.set ,)
self.attrWidget_lDisplay.bind('<Double-Button-1>' ,self.funcWidget_noneLDisplayBindDoubleButton1)
self.attrWidget_ysDisplay.config(command=self.attrWidget_lDisplay.yview)
self.attrWidget_xsDisplay.config(command=self.attrWidget_lDisplay.xview)
# func
def func_noneSelectionSet(self ,intFirst ,intLast=None):
self.attrWidget_lDisplay.select_set(intFirst ,intLast)
return None
def func_noneClearSelectionAll(self):
self.attrWidget_lDisplay.selection_clear(0 ,tk.END)
return None
def func_noneReplaceRowByIndex(self ,intIndex ,strValue):
self.attrWidget_lDisplay.delete(intIndex)
self.attrWidget_lDisplay.insert(intIndex ,strValue)
return None
def func_noneRefreshByCurPath(self):
strCurPath = self.__attrElseBar_barTop.func_strGetCurPath()
#
self.attrWidget_lDisplay.delete(0 ,tk.END)
for strTop ,listFolders ,listFiles in os.walk(strCurPath):
for strFolder in listFolders:
self.attrWidget_lDisplay.insert(tk.END ,strFolder)
for strFile in listFiles:
self.attrWidget_lDisplay.insert(tk.END ,strFile)
break
return None
# forMain
def funcMain_noneAddElseBar(self ,barTop):
self.__attrElseBar_barTop = barTop
return None
def funcMain_noneFirstRun(self):
self.func_noneRefreshByCurPath()
return None
def funcMain_noneGrid(self ,*args ,**kwargs):
self.attrWidget_F.grid(*args ,**kwargs)
self.attrWidget_F.grid_rowconfigure([10] ,weight=1)
self.attrWidget_F.grid_columnconfigure([10] ,weight=1)
#
self.attrWidget_lDisplay.grid(in_=self.attrWidget_F ,row=10 ,column=10 ,sticky='wesn')
self.attrWidget_ysDisplay.grid(in_=self.attrWidget_F ,row=10 ,column=20 ,sticky='sn')
self.attrWidget_xsDisplay.grid(in_=self.attrWidget_F ,row=20 ,column=10 ,sticky='we')
return None
# forwidget
def funcWidget_noneLDisplayBindDoubleButton1(self ,evnet=None):
strFolder = self.attrWidget_lDisplay.get('active')
strOldPath = self.__attrElseBar_barTop.func_strGetCurPath()
strNewPath = os.path.abspath(strOldPath + os.sep + strFolder)
#
if os.path.isdir(strNewPath):
self.__attrElseBar_barTop.func_noneSetCurPath(strNewPath)
self.__attrElseBar_barTop.func_noneCheckHistory()
self.func_noneRefreshByCurPath()
return None
class InterFace():
def __init__(self):
self.root = tk.Tk()
self.root.geometry('800x500')
# widget
self.barTop = Bar_top()
self.barControl = Bar_control()
self.barDisplt = Bar_display()
# addElseBar
self.barTop.funcMain_noneAddElseBar(self.barDisplt)
self.barControl.funcMain_noneAddElseBar(self.barTop ,self.barDisplt)
self.barDisplt.funcMain_noneAddElseBar(self.barTop)
# firstRun
self.barTop.funcMain_noneFirstRun()
self.barControl.funcMain_noneFirstRun()
self.barDisplt.funcMain_noneFirstRun()
# grid
self.root.grid_rowconfigure([20] ,weight=1)
self.root.grid_columnconfigure([20] ,weight=1)
#
tk.Frame(height=5).grid(in_=self.root ,row=10 - 1 ,column=0)
self.barTop.funcMain_noneGrid(in_=self.root ,row=10 ,column=10 ,sticky='we' ,columnspan=1 + 10)
tk.Frame(height=5).grid(in_=self.root ,row=10 + 1 ,column=0)
self.barControl.funcMain_noneGrid(in_=self.root ,row=20 ,column=10 ,sticky='wsn')
self.barDisplt.funcMain_noneGrid(in_=self.root ,row=20 ,column=20 ,sticky='wesn')
#
self.root.mainloop()
if __name__ == '__main__':
InterFace()
我来回答
评分
查看全部评分