#壓縮軟體
#設計界面
import tkinter
import tkinter.filedialog
import zipfile
import os
import tkinter.messagebox
root = tkinter.Tk()
root.title('壓縮軟體1.0')
root.minsize(300,400)
#設定需要壓縮路徑變量
zipfilename = []
#添加檔案的函數
def addfile():
#全局化變量
global zipfilename
#彈出檔案選框
files = tkinter.filedialog.askopenfilenames(title = '請選擇需要亞索的檔案')
#将選擇的檔案加入清單中
zipfilename += list(files)
#将資訊組成字元串書寫
filesstr = '\n'.join(zipfilename)
#将檔案的資訊寫入lable顯示
lable['text'] = filesstr
#壓縮檔案函數
def zip_file():
path = '/home/caohaifeng/PycharmProjects/text.zip'
#打開或者建立壓縮檔案
zp = zipfile.ZipFile(path,'w')
#添加檔案
for filename in zipfilename:
zp.write(filename,os.path.basename(filename))
#關閉壓縮檔案
zp.close()
#判斷壓縮檔案是否建立成功
if os.path.exists(path):
tkinter.messagebox.showinfo(title = '資訊',message = '檔案壓縮成功:' + path )
else:
tkinter.messagebox.showerror(title = '錯誤',message = '壓縮檔案失敗!')
#解壓檔案函數
def unzip_file():
#選擇需要解壓的檔案
zipfilepath = tkinter.filedialog.asksaveasfilename()
#選擇解壓的路徑
unzipfilepath = tkinter.filedialog.askdirectory()
#解壓操作
zp = zipfile.ZipFile(zipfilepath)
#解壓所有
zp.extractall(unzipfilepath)
#關閉檔案
zp.close()
#擺放按鈕
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)
#顯示資訊區域
lable = tkinter.Label(root,text = '沒有檔案資訊',bg = '#abcdef',anchor = 'nw',justify = 'left')
lable.place(x = 20, y = 70,width = 260 , height = 310)
root.mainloop()