I am trying to make a youtube video downloader that searches, shows you the results and when you click on one, downloads the video. I am using pytube for youtube connection and tkinter for GUI. But I am facing this problem: When I click on a when I click on a video another search result gets downloaded. For example search results are, A, B, C, D, E when I click on B it downloads D. Here is the code:
import pytube
import tkinter as tk
import tkinter.font as font
root = tk.Tk()
root.title("Video Downloader")
root.geometry('1000x1000')
entry = tk.Entry(root, width=400, font=("Tahoma 30"))
entry.pack()
def Download(link):
youtubeObject = pytube.YouTube(link)
youtubeObject = youtubeObject.streams.get_highest_resolution()
try:
youtubeObject.download()
except:
print("An error has occurred")
print("Download is completed successfully")
def ComputeMinuteLenght(num):
if num//60 == 0:
return "<1 minute"
elif num//60 == 1:
return "1 minute"
else:
str(num//60)+" minutes"
def ButtonFunc():
s = pytube.Search(entry.get())
for v in s.results:
answerButton = tk.Button(root, text=v.title + "-> "+ComputeMinuteLenght(v.length), command=lambda: Download(v.watch_url))
answerButton['font'] = font.Font(size=15)
answerButton.pack()
SubmitButton = tk.Button(root, text="Show Results", command=ButtonFunc)
SubmitButton['font'] = font.Font(size=30)
SubmitButton.pack()
root.mainloop()
I tried to change v to v+5 or etc. but it doesn't consistently download the search result that is five results away and etc.
I am not able to code right now but I think the problem is at ButtonFunc
lambda "command=lambda: Download(v.watch_url)"
You can update it to:
for v in s.results:
answerButton = tk.Button(root, text=v.title + "-> "+ComputeMinuteLenght(v.length), command=lambda vid=v: Download(vid.watch_url))
answerButton['font'] = font.Font(size=15)
answerButton.pack()
I am out of town and not able to use my pc therefore I didn't tried this but I think this is the problem, you can check.