用python實現基本壓縮功能__python

來源:互聯網
上載者:User
#壓縮軟體#設計介面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()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.