#压缩软件
#设计界面
import os
import zipfile
import tkinter
import tkinter.filedialog
import tkinter.messagebox
global path
path = '/home/hanbing/test.zip'
root = tkinter.Tk()
root.title('myZipfile1.0')
root.minsize(300,400)
#设置需要压缩的文件路径变量
zipfilename = []
#添加文件的函数
def addfile():
# 全局化变量
global zipfilename
#弹出文件选框
files = tkinter.filedialog.askopenfilenames(title = '请选择需要压缩的文件')
#将选择的文件加入列表中
zipfilename += list(files)
#将信息组成字符串书写
filesstr = '\n'.join(zipfilename)
#将文件信息写入label显式
label['text'] = filesstr
#压缩文件函数
def zip_file():
try:
path = '/home/hanbing/test.zip'
# 判断压缩文件是否创建成功
if os.path.exists(path):
tkinter.messagebox.showinfo(title='信息', message='文件压缩成功:' + path)
else:
tkinter.messagebox.showerror(title='错误', message='压缩文件创建失败!' + path)
except PermissionError:
path = '/home/hanbing/test.zip'
if os.path.exists(path):
tkinter.messagebox.showinfo(title='信息', message='文件压缩成功:' + path)
else:
tkinter.messagebox.showerror(title='错误', message='压缩文件创建失败!' + path)
#打开或者创建压缩文件
zp = zipfile.ZipFile(path,'w')
#添加文件
for filename in zipfilename:
zp.write(filename,os.path.basename(filename))
#关闭压缩文件
zp.close()
#解压文件函数
def unzip_file():
#选择需要解压的文件
zipfilepath = tkinter.filedialog.askopenfilename()
#选择解压的路径
unzipfilepath = tkinter.filedialog.askdirectory()
#解压操作
zp = zipfile.ZipFile(zipfilepath)
#解压所有
zp.extractall(unzipfilepath)
#关闭文件
zp.close()
#判断是否解压成功
if os.path.exists(path):
tkinter.messagebox.showinfo(title='信息', message='文件解压成功:' + path)
else:
tkinter.messagebox.showerror(title='错误', message='压缩文件解压失败!' + path)
#摆放按钮
btn_add = tkinter.Button(root,text = '添加文件',command = addfile)
btn_add.place(x = 20,y = 20)
btn_zip = tkinter.Button(root,text = '压缩文件',command = zip_file)
btn_zip.place(x = 110 ,y =20)
btn_unzip = tkinter.Button(root,text = '解压文件',command = unzip_file)
btn_unzip.place(x = 200 , y =20)
#显示信息区域
label = tkinter.Label(root,text = '暂时没有文件信息',bg = 'white',anchor = 'nw',justify = 'left')
label.place(x = 20, y = 70,width = 260 , height = 310)
root.mainloop()
兄弟连学python
Python学习交流、资源共享群:563626388 QQ
-- 简单的压缩解压程序&spm=1001.2101.3001.5002&articleId=78930755&d=1&t=3&u=d1b8e80cc9d641c7abec205cfd9276c6)
5223

被折叠的 条评论
为什么被折叠?



