zoukankan      html  css  js  c++  java
  • 课堂笔记 ---- 面向对象压缩软件

    import tkinter import tkinter.filedialog import zipfile import os import tkinter.messagebox #压缩软件 class zipc: #界面布局方法 def __init__(self): #创建主界面 并保存到成员属性中 self.root = tkinter.Tk() self.root.title('压缩软件1.0') self.root.minsize(300,400) #声明一个label使用的变量 self.filenames = tkinter.StringVar() #界面布局 self.Toast() self.root.mainloop() #界面布局函数 def Toast(self): #界面布局 btn1 = tkinter.Button(self.root,text = '选择文件',command = self.changefile) btn1.place(x=20,y=20,width = 80,height = 40) btn2 = tkinter.Button(self.root,text = '压缩文件',command = self.zipfiles) btn2.place(x=110,y=20,width = 80,height = 40) btn3 = tkinter.Button(self.root,text = '解压文件',command = self.dzipfile) btn3.place(x=200,y=20,width = 80,height = 40) lable = tkinter.Label(self.root,bg = 'white',textvariable = self.filenames,anchor = 'nw',justify='left') lable.place(x=20,y=75,width = 260,height = 300) #选择文件函数 def changefile(self): # 使用打开文件对话框 self.files = tkinter.filedialog.askopenfilenames(title='请打开文件') # 准备显示信息 # 临时路径容器 tmpfile = [] # 遍历元组进行路径的截取和省略操作 for i in self.files: # 判断路径字符格式是否超过指定长度 if len(i) >= 40: i = i[0:15] + '...' + i[-15:] # 将处理之后的数据存入临时列表 tmpfile.append(i) # 将所有文件路径组成字符串写入label filestr = ' '.join(tmpfile) self.filenames.set(filestr) # 压缩文件函数 def zipfiles(self): # 获取压缩后文件的保存路径 filename = tkinter.filedialog.asksaveasfilename(title = '保存文件',filetypes = (('zip文件','*.zip'),('所有文件','*.*'))) # 新建压缩文件 zp = zipfile.ZipFile(filename,'a',zipfile.ZIP_DEFLATED) # 添加要压缩的文件 for onefile in self.files: zp.write(onefile,os.path.basename(onefile)) # 关闭压缩文件 zp.close() # 提示用户压缩路径 tkinter.messagebox.showinfo(title = '操作结果',message ='压缩成功'+filename) # 解压文件函数 def dzipfile(self): # 遍历要解压的文件 for onename in self.files: # 打开要解压的文件 zp = zipfile.ZipFile(onename, 'r') # 获取解压后文件的保存路径 filename = tkinter.filedialog.asksaveasfilename(title='保存文件', filetypes=(('所有文件', '*.*'),)) # 解压全部文件 zp.extractall(filename) # 关闭压缩文件 zp.close() #提示用户解压路径 tkinter.messagebox.showinfo(title='操作结果', message='压缩成功' + filename) #实例化对象 aa = zipc()
  • 相关阅读:
    什么是 bean 的自动装配?
    什么是 Spring 的内部 bean?
    什么是 Spring 的 MVC 框架?
    Spring AOP and AspectJ AOP 有什么区别?
    解释 JDBC 抽象和 DAO 模块?
    volatile 类型变量提供什么保证?
    一个 Spring Bean 定义 包含什么?
    什么是 Spring MVC 框架的控制器?
    使用 Spring 访问 Hibernate 的方法有哪些?
    什么是 Callable 和 Future?
  • 原文地址:https://www.cnblogs.com/mmf1/p/7944136.html
Copyright © 2011-2022 走看看