Search code examples
pythonpython-3.xuser-interfacetkinter

Making a GUI File Downloader with Python 3 and Tkinter


(I am using python 3.6.6 if that matters to anyone)

I am making a GUI installer for a game that is currently in private alpha and is constantly updating.

I already made a console version:

from tqdm import tqdm
import requests, os, sys, zipfile, shutil, subprocess

chunk_size = 1024
url = "{LINK TO FILE YOU WANT TO DOWNLOAD}"
r = requests.get(url, stream = True)
total_size = int(r.headers['content-length'])


print("Are you sure you want to download the newest version of RFMP?")
print("y/n", end=': ')

answer = input()

while True:
    if answer == 'y':
        if os.path.exists("RFMB6_WINDOWS"):
            print('')
            print('')
            print('Removing old RFMP files...')
            subprocess.check_call(('attrib -R ' + 'RFMB6_WINDOWS' + '\\* /S').split())
            shutil.rmtree('RFMB6_WINDOWS')
            print('')
            print('Removed old files.')
            break
        else:
            break
    elif answer == 'n':
         sys.exit()

    else:
         print("That is not a valid answer, please answer with y/n.")
         answer = input()


print('')
print('')
print('Downloading:')
with open('RFMB6_WINDOWS.zip', 'wb') as f:
    for data in tqdm(iterable = r.iter_content(chunk_size = chunk_size), total = total_size/chunk_size, unit = 'KB'):
             f.write(data)
print('')
print("Download Complete.")
print('')
print('')

print("Would you like to extract it?")
print("y/n", end=': ')
answer2 = input()

while True:
    if answer2 == 'y':
            print('')
            print('')
            print('Extracting...')
            zip_ref = zipfile.ZipFile("RFMB6_WINDOWS.zip", 'r')
            zip_ref.extractall("RFMB6_WINDOWS")
            zip_ref.close()
            print('')
            print('Extraction Complete')
            print('')
            print('')
            print('Cleaning up...')
            os.remove("RFMB6_WINDOWS.zip")
            print('')
            print('Done! You have succesfully installed the newest version of the Ravenfield Multiplayer Private Alpha.')
            break   
    elif answer2 == 'n':
            print('')
            print('Done! You have succesfully downloaded the newest Zip of the Ravenfield Multiplayer Private Alpha.')
            break
    else:
            print("That is not a valid answer, please answer with y/n.")
            answer = input()


os.system('pause')

I will only be using this to download 1 specific link so ignore the url variable.

I am trying to make a GUI that does the same thing when I click a button that says 'Download'. I want to make a progress bar, and a text box that tells you what is going on e.g. Downloading, extracting etc. I have no need for a directory option. I just need it to download where ever the file is located and delete the old file if it is still there.

So here is my question: How do I learn how to do this? I have looked at tkinter tutorials and other questions but I only find stuff for python 2 or stuff that is to developed to modify and call my own work. What I am looking for are links and/or examples that can tell me how I go about creating something like this. Thanks in advance to anyone who helps me out.

P.S. I am a noob when it comes to coding so whatever you explain please do it thoroughly.

P.S.S. In order to run the console application you need to run it through terminal and add your own link in the 'url' variable.


Solution

  • Take a look at PySimpleGUI. You can build a layout with a download button, an output window and a progress bar easily. Stop by the GitHub and post an issue if you run into trouble.