#壓縮軟體#設計介面import tkinterimport tkinter.filedialogimport zipfileimport osimport tkinter.messageboxroot = 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()