python-9 简易压缩软件

import tkinter
import tkinter.filedialog
import zipfile
import os
import tkinter.messagebox
class Ysuo:
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title('我的压缩软件')
        self.root.minsize(300, 300)
        self.root.maxsize(400, 400)
        self.set_window()
        # 声明一个列表用于接收文件路径
        self.messages = []
        self.root.mainloop()
    def set_window(self):
        self.open_file = tkinter.Button(self.root, text='打开文件', command=self.open__file)
        self.open_file.place(relx=0.1, rely=0.1, relwidth=0.2, relheight=0.1)

        self.zip_file = tkinter.Button(self.root, text='压缩文件', command=self.zip__file)
        self.zip_file.place(relx=0.4, rely=0.1, relwidth=0.2, relheight=0.1)

        self.unzip_file = tkinter.Button(self.root, text='解压文件', command=self.unzip__file)
        self.unzip_file.place(relx=0.7, rely=0.1, relwidth=0.2, relheight=0.1)

        self.label = tkinter.Label(self.root, bg='white', text='', anchor='nw', justify='left')
        self.label.place(relx=0.1, rely=0.25, relwidth=0.8, relheight=0.65)

    def open__file(self):

        # 弹出文件对话框(用于选择压缩文件)
        files = tkinter.filedialog.askopenfilenames(title='请选择要压缩的文件')
        # 把选择的文件路径添加到列表
        self.messages += list(files)
        # 将列表内容组合成字符串
        ap = '\n'.join(self.messages)
        # 让标签label 显示字符串内容
        self.label['text'] = ap

    def zip__file(self):
        # 创建一个压缩文件(指定路径)
        # path = 'C:/Users/83810/PycharmProjects/untitled/yasuo.zip'
        path = tkinter.filedialog.askdirectory()
        # print(path)
        path1 = 'yasuo.zip'
        lujing = os.path.join(path, path1)
        print(lujing)
        # print(lujing)

        # 创建(或打开)压缩文件
        ap = zipfile.ZipFile(lujing, 'w')
        # 遍历(选择文件)列表内容
        for i in self.messages:
            # 将遍历的文件写入压缩文件
            ap.write(i, os.path.basename(i))
        # 关闭压缩文件
        ap.close()
        # 判断文件是否压缩成功
        if os.path.exists(path):
            tkinter.messagebox.showinfo(title='信息', message='文件压缩成功')
        else:
            tkinter.messagebox.showerror(title='错误', message='文件压缩失败')

    def unzip__file(self):
        # 创建指定选择解压文件的窗口并将选择的解压文件路径储存到zipfilepath
        zipfilepath = tkinter.filedialog.askopenfilename()
        if os.path.exists(zipfilepath):
            # 创建指定解压路径的窗口 并将路径储存到unzipfilepath
            unzipfilepath = tkinter.filedialog.askdirectory()
            if os.path.exists(unzipfilepath):
                zp = zipfile.ZipFile(zipfilepath)
                # 通过解压函数解压文件到指定路径
                zp.extractall(unzipfilepath)
                # 关闭解压文件
                zp.close()
            else:
                return

        else:
            return
       
        # 判断解压是否成功
        if os.path.exists(unzipfilepath):
            tkinter.messagebox.showinfo(title='信息', message='文件解压成功')
        else:
            tkinter.messagebox.showerror(title='信息', message='文件解压失败')
a = Ysuo()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值