Welcome folks today in this blog post we will be building a tkinter desktop app in which we will be downloading pdf documents from url
. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the following libraries using the pip
command into your python project
pip install tkinter
After installing this library you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
from tkinter import ttk from tkinter import * import time import requests from tqdm import tqdm import os from tkinter import messagebox as m_box import re win = Tk() win.title("PDF Downloader") win.geometry("500x360") win.minsize(500,360) win.maxsize(500,360) frame = ttk.LabelFrame(win, width=290) frame.pack(padx=30, pady=90) label1 = ttk.Label(frame, text="Enter The File URL : ") label1.grid(row=0, column=0,sticky=W) url = StringVar() edit_txt = ttk.Entry(frame, width=50, textvariable=url) edit_txt.grid(row=1, columnspan=4, padx=2, pady=3) label2 = ttk.Label(frame, text="Enter The File Name : ") label2.grid(row=2, column=0,sticky=W) filename = StringVar() file_name = ttk.Entry(frame, width=50, textvariable=filename) file_name.grid(row=3, columnspan=4, padx=2, pady=3) def onClick(): os.chdir(r"C:\Users\geeky\Desktop\Projects\qrcode\Tkinter_Screen_Recorder\PDF-Downloader-GUI-Tkinter-Python") file_url = url.get() file_name_aft_click = filename.get() if re.search(".pdf", file_url): if (os.path.exists(file_name_aft_click + ".pdf") == False): r = requests.get(file_url, stream= True) total_size = int(r.headers['content-length']) status = Label(win, text="Downloading PDF File...", font="Arial 11", relief=SUNKEN, anchor=W) status.pack(side=BOTTOM, fill=X) with open(file_name_aft_click + ".pdf", 'wb') as f: for data in tqdm(iterable=r.iter_content(chunk_size = 1024), total=total_size/1024, unit="KB"): f.write(data) m_box.showinfo("Download Compleated","Your File Successfully Downloaded") status.forget() else: m_box.showerror("Error","File Name Already Exist in Downloading Path !") else: m_box.showerror("Error", "Only PDF Files are Allowed to Download !") btn1 = ttk.Button(frame, width=30, text="Download File", command=onClick) btn1.grid(row=4, columnspan=4, padx=30, pady=5) win.mainloop() |
Now if you execute the python application by typing below command
python app.py