Python(9) --实现一个简单的压缩软件/解压软件的功能

#压缩软件

#导入所需模块,设置界面
import os
import zipfile
import tkinter
import tkinter.messagebox
import tkinter.filedialog

root = tkinter.Tk()
root.title('我的压缩软件')
root.minsize(300,400)

#设置需要压缩文件的路径变量
filenames = []

#添加文件的函数
def addfiles():
    #全局化变量
    global filenames
    #弹出文件选择对话框,选择需要添加的文件
    files = tkinter.filedialog.askopenfilenames()
    #将选择的文件添加到列表filenames中
    filenames += list(files)
    #将列表中的数据连接成字符串
    strs = '
'.join(filenames)
    #将需要压缩文件的路径写入标签中显示
    label['text'] = strs
    #判断是否添加了文件,显示添加文件的个数
    global num
    num = strs.count('
') + 1
    if filenames == []:
        tkinter.messagebox.showinfo(title='提示信息', message='您好,您还未添加任何文件')
    else:
        tkinter.messagebox.showinfo(title='提示信息', message='您好,您共添加了{}个文件'.format(num))


#压缩文件的函数
def zip_files():
    #创建压缩文件的目录变量
    global path
    path = './myzip.zip'

    #提示是否需要对当前文件进行压缩
    result = tkinter.messagebox.askokcancel(title='提示信息', message='是否需要对当前{}个文件进行压缩?'.format(num))
    #根据用户选择是否需要创建压缩文件
    if result == True:
        # 创建压缩文件
        zp = zipfile.ZipFile(path, 'w')
        # 向压缩文件中添加文件内容
        #遍历添加文件的列表
        for file in filenames:
            global dir
            dir = os.path.basename(file)
            zp.write(file,dir)
        # 关闭压缩文件
        zp.close()

    #判断压缩文件是否创建成功
    if os.path.exists(path):
        tkinter.messagebox.showinfo(title = '提示信息',message = '压缩文件创建成功!!!
目录为:'+ path)
    else:
        tkinter.messagebox.showerror(title = '错误信息',message = '压缩文件创建失败!!!')

#解压文件的函数
def unzip_files():

    try:
        # 选择解压文件夹
        tkinter.messagebox.showinfo(title='提示信息', message='请选择选择解压文件!!!')
        unzipfile = tkinter.filedialog.askopenfilename()
        #提示用户是否解压当前文件
        result = tkinter.messagebox.askokcancel(title='提示信息', message='是否需要对当文件进行解压?')
        if result == True:
            # 选择解压的路径
            tkinter.messagebox.showinfo(title='提示信息', message='请选择选择解压路径!!!')
            unzippath = tkinter.filedialog.askdirectory()
            # 打开解压文件夹
            unzp = zipfile.ZipFile(unzipfile)
            # 解压所有文件
            unzp.extractall(unzippath)
            # 关闭压缩文件
            unzp.close()
            tkinter.messagebox.showinfo(title='提示信息', message='解压文件成功!!!')
        else:
            tkinter.messagebox.showinfo(title='提示信息', message='您已取消解压,
解压文件失败!!!')


    # 判断解压是否成功
    except:
        tkinter.messagebox.showerror(title='错误信息', message='系统程序错误,
解压文件失败!!!')








#摆放按钮组件
btn_add = tkinter.Button(root,text = '添加文件',command = addfiles)
btn_add.place(x = 10,y = 20 )

btn_zip = tkinter.Button(root,text = '压缩文件',command = zip_files)
btn_zip.place(x = 110,y = 20)

btn_unzip = tkinter.Button(root,text = '解压文件',command = unzip_files)
btn_unzip.place(x = 210,y = 20)

#显示信息区域
label = tkinter.Label(root,text = '暂时没有文件信息',bg = 'white',anchor = 'nw',justify = 'left')
label.place(x = 10,y = 70 ,width = 280,height = 300)



root.mainloop()

  

原文地址:https://www.cnblogs.com/w-yumiao/p/8231005.html