Use Python to write your own Simple zip compression software with a complete picture (suitable for beginners), pythonzip

Source: Internet
Author: User
Tags decompress file uncompress

Use Python to write your own Simple zip compression software with a complete picture (suitable for beginners), pythonzip
I. Software Description

Use the Python tkinter module to write your own compression software. The zip file format is a common document compression standard. In the ziplib module, ZipFile is used to operate zip files. It has the following functions: zip compression and zip decompression. (Fewer features, relatively low, Do not spray .)

Ii. required knowledge points
  • Use of functions
  • Use of global variables
  • Tkinter Module
  • Zip Module
3. Program Implementation Process Analysis

 

4. The Code is as follows:
1 import tkinter 2 import tkinter. filedialog 3 import OS 4 import zipfile 5 import tkinter. messagebox 6 7 # create a window 8 root = tkinter. tk () 9 root. minsize (700,500) 10 root. title ('Li Yunlong's compression software ') 11 root ['bg'] = '#303030 '12 13 14 15 16 # declare a global variable files 17 files = () 18 # declare the lable variable 19 filenames = tkinter. stringVar () 20 #1. select File Operation 21 def selecfiles (): 22 # declare global variable 23 global files 24 # use the file dialog box to select File 25 files = tkinter. filedialog. askopenfilenames (title = 'select the software bitch') 26 # display the information of the selected file 27 # temporary path container 28 tmpfiles = [] 29 for I in files: 30 if len (I)> 60: 31 I = I [0: 20] + '... '+ I [-15:] 32 tmpfiles. append (I) 33 filestr = '\ n '. join (tmpfiles) 34 print (filestr) 35 filenames. set (filestr) # display the file name 36 37 38 39 40 #2. compressed file function 41 def zipfiles (): 42 global files 43 # obtain the compressed file path 44 filename = tkinter. filedialog. asksaveasfilename (title = 'Save file', filetypes = ('zip file ','*. zip '), ('all files ','*. * ') 45 # create a compressed file 46 zp = zipfile. zipFile (filename }'.zip ', 'A') # default zip format for compressed files 47 # add files to be compressed (traversal operation 48 for onefiles in files: 49 zp. write (onefiles, OS. path. basename (onefiles) 50 zp. close () #51 after creation # Prompt User compression path 52 tkinter. messagebox. showinfo (title = 'Operation result', message = 'compressed successfully: '+ filename) 53 54 55 56 57 #3. decompression function 58 def uncompress (): 59 global files 60 61 # use the file dialog box to select File 62 files = tkinter. filedialog. askopenfilenames (title = 'select the software bitch') 63 # display the information of the selected file 64 # temporary path container 65 tmpfiles = [] 66 for I in files: 67 if len (I)> 60: 68 I = I [0: 20] + '... '+ I [-15:] 69 tmpfiles. append (I) 70 filestr = '\ n '. join (tmpfiles) 71 print (filestr) 72 filenames. set (filestr) 73 74 75 zp = zipfile. zipFile (filestr, 'R') 76 # Add the file to be compressed (traverse operation 77 # for onefiles in files: 78 files1 = tkinter. filedialog. askdirectory (title = 'select the path you want to decompress ') 79 zp. extractall (files1) 80 zp. close () # unzip 81 # Prompt User compression path 82 tkinter. messagebox. showinfo (title = 'Operation result', message = 'decompressed successfully: '+ files1) 83 84 85 86 87 88 89 90 91 # UI layout 92 # Menu Bar 93 allmenu = tkinter. menu (root, bg = 'black') 94 95 filmenu = tkinter. menu (allmenu, tearoff = 0) 96 filmenu. add_command (label = 'open ') 97 filmenu. add_command (label = 'save') 98 filmenu. add_separator () 99 filmenu. add_command (label = 'settings') 100 filmenu. add_command (label = 'logout ') 101 102 filmenu1 = tkinter. menu (allmenu, tearoff = 0) 103 filmenu1.add _ command (label = 'open ') 104 filmenu1.add _ command (label = 'save') 105 filmenu1.add _ separator () 106 filmenu1.add _ command (label = 'settings') 107 filmenu1.add _ command (label = 'logout ') 108 109 110 111 allmenu. add_cascade (label = 'file', menu = filmenu) 112 allmenu. add_cascade (label = 'editor', menu = filmenu1) 113 allmenu. add_cascade (label = 'tool') 114 115 root. config (menu = allmenu, bg = 'black') 116 117 118 # Add button interface 119 label = tkinter. label (root, bg = '#242424') 120 label. place (width = 700, height = 115) 121 #1. add file button 122 btnadd = tkinter. button (root, text = 'select file', bg = '#242424', bd = 0.5, fg = 'Gray ', command = selecfiles) 123 btnadd. place (x = 100, y = 70, width = '80', height = 30) 124 #2. the compression OPERATION button is 125 btnadd = tkinter. button (root, text = 'compressed file', bg = '#242424', bd = 0.5, fg = 'Gray ', command = zipfiles) 126 btnadd. place (x = 300, y = 70, width = '80', height = 30) 127 #3. unzip the operation button 128 btnadd = tkinter. button (root, text = 'decompress file', bg = '#242424', bd = 0.5, fg = 'Gray ', command = uncompress) 129 btnadd. place (x = 500, y = 70, width = '80', height = 30) 130 131 img1 = tkinter. photoImage (file = '1.gif ') 132 labelg1 = tkinter. label (root, image = img1) 133 labelg1.place (x = 115, y = 15, width = 50, height = 50) 134 135 img2 = tkinter. photoImage (file = '2.gif ') 136 labelg2 = tkinter. label (root, image = img2) 137 labelg2.place (x = 317, y = 15, width = 50, height = 50) 138 139 img3 = tkinter. photoImage (file = '1.gif ') 140 labelg3 = tkinter. label (root, image = img1) 141 labelg3.place (x = 515, y = 15, width = 50, height = 50) 142 143 144 #4 components that display information 145 label = tkinter. label (root, bg = '# F2F2F2', textvariable = filenames, anchor = 'nw ', justify = 'left') 146 label. place (x = 5, y = 115, width = '000000', height = '000000') 690 370 147 root. mainloop ()

 

 

5. Complete

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.