class YSRJ:
import tkinter
import tkinter.filedialog
import zipfile
import os.path
def __init__(self):
import tkinter.messagebox
self.root = tkinter.Tk()
self.root.title('我的压缩软件')
self.root.minsize(300, 400)
butadd = tkinter.Button(self.root, text='添加文件', command=self.add_zip)
butadd.place(x=20, y=20)
butzip = tkinter.Button(self.root, text='压缩文件', command=self.zip_file)
butzip.place(x=110, y=20)
butunzip = tkinter.Button(self.root, text='解压文件', command=self.unzip_file)
butunzip.place(x=200, y=20)
self.label = tkinter.Label(text='请添加文件', bg='#abcdef', anchor='nw', justify='left')
self.label.place(x=20, y=60, width=260, height=320)
self.zipfilename = []
self.root.mainloop()
#打开文件操作
def add_zip(self):
filepaths = self.tkinter.filedialog.askopenfilenames(title = '请添加文件')
try:
self.zipfilename += list(filepaths)
ls = '\n'.join(self.zipfilename)
print(self.zipfilename,type(self.zipfilename))
self.label['text'] = ls
except:
self.tkinter.messagebox.showerror(title='错误', message='操作错误!')
#压缩文件操作
def zip_file(self):
try:
path = '/home/pangzhi/test.zip'
zp= self.zipfile.ZipFile(path,'w')
for name in self.zipfilename:
zp.write(name,self.os.path.basename(name))
zp.close()
self.tkinter.messagebox.showinfo(title = '提示',message='文件压缩成功')
except:
self.tkinter.messagebox.showerror(title='错误', message='操作错误!')
#解压文件操作
def unzip_file(self):
try:
#选择解压文件
unzip =self.tkinter.filedialog.askopenfilename(title = '请选择文件',)
#选择解压路径
unzippath = self.tkinter.filedialog.askdirectory(title = '解压到……')
#执行解压操作
unzip = self.zipfile.ZipFile(unzip)
#解压所有
unzip.extractall(unzippath)
#关闭
unzip.close()
self.tkinter.messagebox.showinfo(title = '提示',message='解压完成')
except:
self.tkinter.messagebox.showerror(title='错误', message='操作错误!')
ll = YSRJ()
import tkinter
import tkinter.filedialog
import zipfile
import os.path
def __init__(self):
import tkinter.messagebox
self.root = tkinter.Tk()
self.root.title('我的压缩软件')
self.root.minsize(300, 400)
butadd = tkinter.Button(self.root, text='添加文件', command=self.add_zip)
butadd.place(x=20, y=20)
butzip = tkinter.Button(self.root, text='压缩文件', command=self.zip_file)
butzip.place(x=110, y=20)
butunzip = tkinter.Button(self.root, text='解压文件', command=self.unzip_file)
butunzip.place(x=200, y=20)
self.label = tkinter.Label(text='请添加文件', bg='#abcdef', anchor='nw', justify='left')
self.label.place(x=20, y=60, width=260, height=320)
self.zipfilename = []
self.root.mainloop()
#打开文件操作
def add_zip(self):
filepaths = self.tkinter.filedialog.askopenfilenames(title = '请添加文件')
try:
self.zipfilename += list(filepaths)
ls = '\n'.join(self.zipfilename)
print(self.zipfilename,type(self.zipfilename))
self.label['text'] = ls
except:
self.tkinter.messagebox.showerror(title='错误', message='操作错误!')
#压缩文件操作
def zip_file(self):
try:
path = '/home/pangzhi/test.zip'
zp= self.zipfile.ZipFile(path,'w')
for name in self.zipfilename:
zp.write(name,self.os.path.basename(name))
zp.close()
self.tkinter.messagebox.showinfo(title = '提示',message='文件压缩成功')
except:
self.tkinter.messagebox.showerror(title='错误', message='操作错误!')
#解压文件操作
def unzip_file(self):
try:
#选择解压文件
unzip =self.tkinter.filedialog.askopenfilename(title = '请选择文件',)
#选择解压路径
unzippath = self.tkinter.filedialog.askdirectory(title = '解压到……')
#执行解压操作
unzip = self.zipfile.ZipFile(unzip)
#解压所有
unzip.extractall(unzippath)
#关闭
unzip.close()
self.tkinter.messagebox.showinfo(title = '提示',message='解压完成')
except:
self.tkinter.messagebox.showerror(title='错误', message='操作错误!')
ll = YSRJ()