The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a GUI file downloader

Here’s a complete Python example of a simple GUI file downloader using Tkinter for the interface and requests to download files from URLs. This example includes:

  • Input field for the file URL

  • Button to choose download location

  • Button to start download

  • Progress bar to show download progress

  • Status label to show current status or errors

python
import tkinter as tk from tkinter import ttk, filedialog, messagebox import requests import threading import os class FileDownloader(tk.Tk): def __init__(self): super().__init__() self.title("File Downloader") self.geometry("500x200") self.resizable(False, False) self.url_label = tk.Label(self, text="File URL:") self.url_label.pack(pady=(10, 0)) self.url_entry = tk.Entry(self, width=60) self.url_entry.pack(pady=5) self.path_label = tk.Label(self, text="Save to:") self.path_label.pack(pady=(10, 0)) self.path_var = tk.StringVar() self.path_entry = tk.Entry(self, textvariable=self.path_var, width=50, state="readonly") self.path_entry.pack(side=tk.LEFT, padx=(20, 0), pady=5) self.browse_button = tk.Button(self, text="Browse", command=self.browse_folder) self.browse_button.pack(side=tk.LEFT, padx=10, pady=5) self.download_button = tk.Button(self, text="Download", command=self.start_download) self.download_button.pack(pady=10) self.progress = ttk.Progressbar(self, orient="horizontal", length=460, mode="determinate") self.progress.pack(pady=(0, 10)) self.status_label = tk.Label(self, text="") self.status_label.pack() def browse_folder(self): folder = filedialog.askdirectory() if folder: self.path_var.set(folder) def start_download(self): url = self.url_entry.get().strip() save_folder = self.path_var.get().strip() if not url: messagebox.showwarning("Input Error", "Please enter a URL.") return if not save_folder: messagebox.showwarning("Input Error", "Please select a folder to save the file.") return self.download_button.config(state="disabled") self.progress["value"] = 0 self.status_label.config(text="Starting download...") threading.Thread(target=self.download_file, args=(url, save_folder), daemon=True).start() def download_file(self, url, save_folder): try: response = requests.get(url, stream=True, timeout=10) response.raise_for_status() total_length = response.headers.get('content-length') if total_length is None: self.status_label.config(text="Cannot retrieve file size.") total_length = 0 else: total_length = int(total_length) filename = url.split("/")[-1] or "downloaded_file" save_path = os.path.join(save_folder, filename) with open(save_path, "wb") as f: downloaded = 0 for chunk in response.iter_content(chunk_size=4096): if chunk: f.write(chunk) downloaded += len(chunk) if total_length: percent = int(downloaded * 100 / total_length) self.progress["value"] = percent self.status_label.config(text=f"Downloading... {percent}%") else: self.status_label.config(text=f"Downloading... {downloaded} bytes") self.status_label.config(text=f"Download completed: {filename}") except requests.exceptions.RequestException as e: self.status_label.config(text=f"Error: {e}") except Exception as e: self.status_label.config(text=f"Error: {e}") finally: self.download_button.config(state="normal") if __name__ == "__main__": app = FileDownloader() app.mainloop()

How it works:

  • Enter the file URL.

  • Choose the folder to save the file.

  • Click “Download” to start.

  • Progress bar and status update during the download.

  • The download runs in a separate thread to keep the GUI responsive.

Let me know if you want this extended with features like pause/resume or multiple downloads!

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About