用tkinter实现一个简单的文件备份工具(一)
tkinter部分参考自:https://blog.csdn.net/u012156341/article/details/89486070?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
文件复制部分参考自:
https://blog.csdn.net/iteye_20793/article/details/82676647###
主界面部分:PyFileSyncTk.py
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog #选择目录
import os
from SyncTool import SyncDirTool #复制文件功能实现模块
class _childWindow(tk.Frame): #按钮,entry,treeview等的框架
#master:父类,_myRow:grid行位置,_myColumn:grid列位置
def __init__(self,master,_myRow,_myColumn): #init前后双下划线
#Frame
_myFrame=tk.Frame(master)
_myFrame.grid(row=_myRow,column=_myColumn)
#按钮,command命令后的函数不能有(),否则按钮初始化的时候就会执行
self._myButton=tk.Button(_myFrame,text="选择目录",command=self._selectPath)
self._myButton.grid(row=0,column=0)
#目录显示Entry
self._myEntry=tk.Entry(_myFrame,bg="#d3d3d3")
self._myEntry.grid(row=0,column=1)
#treeview初始化
self._myTree=ttk.Treeview(_myFrame,show="tree")#"tree"隐藏treeview上的一行空白
self._myTree.grid(row=1,column=0,columnspan=2,sticky="w"+"e"+"n"+"s",ipady=50)
#scrollbar初始化
self._myScrollbar=tk.Scrollbar(_myFrame)
self._myScrollbar.grid(row=1,column=2,sticky="n"+"s")
#scrollbar和treeview的绑定,两个都要声明,不然Srollbar控制或显示会有点小问题
self._myScrollbar.config(command=self._myTree.yview)
self._myTree.config(yscrollcommand=self._myScrollbar.set)
#按钮功能:选择自定义目录并将其文本值写入_myEntry
def _selectPath(self):
#treeview和entry的清空,不清空第二次点按钮内容会叠加
temp=self._myTree.get_children()
for i in temp:
self._myTree.delete(i)
self._myEntry.delete(0,"end")
#获取要备份的目录名,插入主目录再用loadtree插入子目录
_mainPath=filedialog.askdirectory() #返回选择目录,如果不选择目录直接叉掉会报个错
self._myEntry.insert(0,_mainPath) #将目录写入_myEntry
tempPath=os.path.split(self._myEntry.get())#将目录名分成其他部分和最后一个(元组)
_myTreeRoot=self._myTree.insert("","end",text=tempPath[-1],values=(self._myEntry.get()))
self.loadTree(self._myEntry.get(),_myTreeRoot)#插入Treeview根节点
#根据主目录和treeview根节点,将所有子目录及文件名插入Treeview
def loadTree(self,parentPath,root):
#获取一个文件夹当中所有的元素
filelist = os.listdir(parentPath)
for filename in filelist:
absPath = os.path.join(parentPath,filename)#目录+文件名
#插入树枝
treey = self._myTree.insert(root,'end',text=filename,values=(absPath))
#判断路径是否是文件夹
if os.path.isdir(absPath):
self.loadTree(absPath,treey)
#备份按扭及实现备份功能
class SyncWindow(tk.Frame):
def __init__(self,master,_myRow,_myColumn):
_syncFrame=tk.Frame(master)
_syncFrame.grid(row=_myRow,column=_myColumn)
#备份按钮
self.sbutton=tk.Button(_syncFrame,text="点我备份",command=self.copyFunc)
self.sbutton.pack()
def copyFunc(self):
#print("按了备份按钮")
sync=SyncDirTool(w1._myEntry.get(),w2._myEntry.get()) #将目录1中不同的部分复制到目录2
sync.syncDir()
sync2=SyncDirTool(w2._myEntry.get(),w1._myEntry.get()) #将目录2中多的文件复制给目录1
sync2.syncDir()
if __name__=="__main__":
root=tk.Tk()
root.title("PyFileSync By CeresLi")
root.geometry("600x400") #窗口大小
w1=_childWindow(root,0,0)
w2=_childWindow(root,0,1) #创建两个备份用Frame
w3=SyncWindow(root,0,2) #创建备份功能按钮
root.mainloop()
文件复制功能部分:SyncTool.py
import os
import shutil
class SyncDirTool:
#获得要备份的两个目录
def __init__(self,_dir1,_dir2):
self._dir1=_dir1
self._dir2=_dir2
#对备份功能的简单封装
def syncDir(self):
self._copyDir(self._dir1,self._dir2)
#从_dir1向_dir2复制文件
def _copyDir(self,_copyDir1,_copyDir2):
self.generateDir(_copyDir2) #目标目录不存在就创建
for filename in os.listdir(_copyDir1):
_tempDir1=_copyDir1+os.sep+filename
_tempDir2=_copyDir2+os.sep+filename
print("遍历中"+_tempDir1)
#递归复制文件夹+复制文件
if os.path.isdir(_tempDir1):
self._copyDir(_tempDir1,_tempDir2)
else:
self._copyFile(_tempDir1,_tempDir2)
def _copyFile(sef,_cfDir1,_cfDir2):
if not os.path.exists(_cfDir2):
shutil.copy2(_cfDir1,_cfDir2)
print("增加文件%s --> %s"%(_cfDir1,_cfDir2))
def generateDir(self,tempPath):
tempPath=tempPath.strip() #去除首位空格
tempPath=tempPath.rstrip(os.sep) #去除尾部分隔符
#判断路径存在,不存在就创建
pathExists=os.path.exists(tempPath)
if not pathExists:
print(tempPath+"目录创建成功")
os.makedirs(tempPath)
基于python3.8 将代码复制到对应名字的文件下即可运行。
可以实现两个文件夹的同步,子目录内容太多可能会卡住。
后续有时间会继续完善界面和功能。