学python--09--用tkinter做一个压缩软件

这个博客介绍了一个使用Python的tkinter库创建的简单压缩软件。该软件具有添加文件、压缩文件和解压文件的功能。用户可以通过点击按钮选择文件进行压缩或解压,操作完成后会显示相应的提示信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class YSRJ:
    import tkinter
    import tkinter.filedialog
    import zipfile
    import os.path
    def __init__(self):


        import tkinter.messagebox


        self.root = tkinter.Tk()
        self.root.title('我的压缩软件')
        self.root.minsize(300, 400)
        butadd = tkinter.Button(self.root, text='添加文件', command=self.add_zip)
        butadd.place(x=20, y=20)


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


        butunzip = tkinter.Button(self.root, text='解压文件', command=self.unzip_file)
        butunzip.place(x=200, y=20)


        self.label = tkinter.Label(text='请添加文件', bg='#abcdef', anchor='nw', justify='left')
        self.label.place(x=20, y=60, width=260, height=320)


        self.zipfilename = []
        self.root.mainloop()
#打开文件操作
    def add_zip(self):


        filepaths = self.tkinter.filedialog.askopenfilenames(title = '请添加文件')
        try:
            self.zipfilename += list(filepaths)
            ls = '\n'.join(self.zipfilename)
            print(self.zipfilename,type(self.zipfilename))
            self.label['text'] = ls
        except:
            self.tkinter.messagebox.showerror(title='错误', message='操作错误!')


    #压缩文件操作
    def zip_file(self):
        try:
            path = '/home/pangzhi/test.zip'
            zp= self.zipfile.ZipFile(path,'w')
            for name in self.zipfilename:
                zp.write(name,self.os.path.basename(name))
            zp.close()
            self.tkinter.messagebox.showinfo(title = '提示',message='文件压缩成功')
        except:
            self.tkinter.messagebox.showerror(title='错误', message='操作错误!')


    #解压文件操作
    def unzip_file(self):
        try:
            #选择解压文件
            unzip =self.tkinter.filedialog.askopenfilename(title = '请选择文件',)
            #选择解压路径
            unzippath = self.tkinter.filedialog.askdirectory(title = '解压到……')
            #执行解压操作
            unzip = self.zipfile.ZipFile(unzip)
            #解压所有
            unzip.extractall(unzippath)
            #关闭
            unzip.close()
            self.tkinter.messagebox.showinfo(title = '提示',message='解压完成')
        except:
            self.tkinter.messagebox.showerror(title='错误', message='操作错误!')






ll = YSRJ()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值