Skip to content

Commit a18a6ca

Browse files
Update youtubedownloader.py
The use of global variables should be minimized as much as possible. In the current code, url_box is a global variable. Instead, you can pass the url_box as a parameter to the download function. It is good practice to include exception handling in your code, especially when working with web requests. You can add a try-except block around the line url = YouTube(str(url_box.get())) to handle any errors that may occur. You can add a progress bar to show the progress of the download. This will give the user a better idea of how much time is left until the download is complete. You can also add a feature to let the user choose the download location instead of downloading to the default directory.
1 parent ae8d50c commit a18a6ca

File tree

1 file changed

+16
-54
lines changed

1 file changed

+16
-54
lines changed

youtubedownloader.py

+16-54
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
from tkinter import *
2+
from tkinter import filedialog, messagebox
23
from threading import Thread
3-
from tkinter import messagebox
44
from pytube import YouTube
55

66

77
def threading():
88
# Call work function
9-
t1=Thread(target=download)
9+
t1 = Thread(target=download)
1010
t1.start()
1111

1212
def download():
13-
url = YouTube(str(url_box.get()))
14-
video = url.streams.first()
15-
video.download()
16-
messagebox.showinfo('', 'Download completed!')
13+
try:
14+
url = YouTube(str(url_box.get()))
15+
video = url.streams.first()
16+
filename = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("MP4 files", "*.mp4")])
17+
video.download(filename=filename)
18+
messagebox.showinfo('', 'Download completed!')
19+
except Exception as e:
20+
messagebox.showerror("Error", "An error occurred while downloading the video.")
1721

18-
root =Tk()
22+
23+
root = Tk()
1924
root.title('YouTube Downloader')
2025
root.geometry('780x500+200+200')
21-
#root.iconbitmap('youtube.ico')
2226
root.configure(bg='olivedrab1')
23-
root.resizable(False,False)
27+
root.resizable(False, False)
2428

2529
# Label widgets
2630
introlable = Label(
@@ -29,9 +33,9 @@ def download():
2933
width=30,
3034
relief='ridge',
3135
bd=4,
32-
font=('chiller',26,'italic bold'),
36+
font=('chiller', 26, 'italic bold'),
3337
fg='red')
34-
introlable.place(x=35,y=20)
38+
introlable.place(x=35, y=20)
3539

3640
Label(
3741
root,
@@ -40,55 +44,13 @@ def download():
4044
bg='olivedrab1'
4145
).place(x=40, y=150)
4246

43-
# DownloadingSizeLabel = Label(
44-
# root,
45-
# text='Total Size: ',
46-
# font=('arial',15,'italic bold'),
47-
# bg='olivedrab1'
48-
# )
49-
# DownloadingSizeLabel.place(x=500,y=240)
50-
51-
# DownloadingLabel = Label(
52-
# root,
53-
# text='Recieved Size: ',
54-
# font=('arial',15,'italic bold'),
55-
# bg='olivedrab1'
56-
# )
57-
# DownloadingLabel.place(x=500,y=290)
58-
59-
# DownloadingTime = Label(
60-
# root,
61-
# text='Time Left : ',
62-
# font=('arial',15,'italic bold'),
63-
# bg='olivedrab1'
64-
# )
65-
# DownloadingTime.place(x=500,y=340)
66-
67-
# DownloadingSizeLabelResult = Label(
68-
# root,
69-
# text=' ',
70-
# font=('arial',15,'italic bold'),
71-
# bg='olivedrab1'
72-
# )
73-
# DownloadingSizeLabelResult.place(x=650,y=240)
74-
75-
# DownloadingLabelResult = Label(
76-
# root,
77-
# text=' ',
78-
# font=('arial',15,'italic bold'),
79-
# bg='olivedrab1'
80-
# )
81-
# DownloadingLabelResult.place(x=650,y=290)
82-
83-
# Entry widgets
8447
url_box = Entry(
8548
root,
86-
font=('arial',30),
49+
font=('arial', 30),
8750
width=30
8851
)
8952
url_box.place(x=40, y=180)
9053

91-
# Button Widgets
9254
btn = Button(
9355
root,
9456
text='DOWNLOAD',

0 commit comments

Comments
 (0)