Home Developer Programming Language Framework Articles Contact Me

HTML CSS JAVASCRIPT JAVA PYTHON JQUERY REACTJS VUEJS ANGULARJS EMBERJS SASS LESS STYLUS GITHUB GITLAB BIGBUCKET JAVA KOTLIN HADOOP JDBC SERVLETS JAVA BEANS(EJB) JAVA SERVER PAGES(JSP) UNIX LINUX POSTGRESQL MSSQLSERVER SQLITE MONGODB XML FLASK PYTHON MYSQL JAVA C++ JUNIT SELENIUM

Unzip & Zip Multiple File Using Python in Tkinter

Prerequisite:-

In this article, we will learn how to extract multiple zips and unzip files using Python in Tkinter.

Libraries Required:-

  1. Tkinter
  2. Zipfile

Installation:-

pip install zipfile

Below is how the GUI looks like:-

Let's Understand Step by Step Implementation:-

Step 1(Create Tkinter Window and add multiple buttons, labels, etc...)

# Import Module
from tkinter import *

# Create Object
root = Tk() 
# set Geometry
root.geometry('400x200')

# Add Labels and Buttons
Label(root, text="Zip & Unzip File", font="italic 15 bold").pack(pady=10)

Button(root,text="Select File",font=14).pack(pady=10)

frame = Frame()
frame.pack(pady=20)

zip_btn = Button(frame,text="UnZip All Files",relief="solid",bg="white",font=15)
zip_btn.pack(side=LEFT,padx=10)

unzip_btn = Button(frame,text="Zip All Files",relief="solid",bg="white",font=15)
unzip_btn.pack()

# Execute Tkinter
root.mainloop()

Step 2 (Define Functions)

We will create three functions;select_file(),unzip_file()& zip_file()

def select_file():
  global file_names
  file_names = askopenfilenames(initialdir = "/",title = "Select File")

# UnZip All Files
def unzip_file():
  for file_name in file_names:
    if file_name.endswith('.zip'):
      with ZipFile(file_name, 'r') as zip:
        zip.extractall()

# Zip The Files
def zip_file():
    with ZipFile('my_python_files.zip','w') as zip:
        for file_name in file_names: 
            zip.write(file_name)

Below is the Implementation:-

# Import Module
from tkinter import *
from tkinter.filedialog import askopenfilenames 
from zipfile import ZipFile 

# Create Object
root = Tk() 
# set Geometry
root.geometry('400x200')

def select_file():
  global file_names
  file_names = askopenfilenames(initialdir = "/",title = "Select File")

# UnZip All Files
def unzip_file():
  for file_name in file_names:
    if file_name.endswith('.zip'):
      with ZipFile(file_name, 'r') as zip:
        zip.extractall()

# Zip The Files
def zip_file():
    with ZipFile('my_python_files.zip','w') as zip:
        for file_name in file_names: 
            zip.write(file_name) 


# Add Labels and Buttons
Label(root, text="Zip & Unzip File", font="italic 15 bold").pack(pady=10)

Button(root,text="Select File",command=select_file,font=14).pack(pady=10)

frame = Frame()
frame.pack(pady=20)

zip_btn = Button(frame,text="UnZip All Files",command=unzip_file,relief="solid",bg="white",font=15)
zip_btn.pack(side=LEFT,padx=10)

unzip_btn = Button(frame,text="Zip All Files",command=zip_file,relief="solid",bg="white",font=15)
unzip_btn.pack()

# Execute Tkinter
root.mainloop()

Output:-