-2

It has to work,but it gives me this error:

File "C:\Users\ХарисВМладенов\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\customtkinter\windows\widgets\ctk_button.py", line 553, in _clicked
    self._command()
TypeError: Download() missing 1 required positional argument: 'url_var'

Here is my code:

import tkinter as tk
import customtkinter as ctk
from pytube import YouTube

def Download(url_var):   
    try:
        ytlink = link.get(url_var)
        ytObject = YouTube(ytlink)
        video = ytObject.streams.get_highest_resolution(url_var)
        video.download(video) 
    except:
        print("Invalid link") 
    print("Download Compleate")
ctk.set_appearance_mode("System")
ctk.set_default_color_theme("blue")

app = ctk.CTk()
app.geometry("720x480")
app.title("YouTube Download")

url_var = tk.StringVar()
title = ctk.CTkLabel(app, text="Insert a YouTube link")
title.pack(padx=10, pady=10)


link = ctk.CTkEntry(app, width=350, height=40, textvariable=url_var)
link.pack()

download = ctk.CTkButton(app, text="Download", command=Download)
download.pack()

app.mainloop()

I tried without the url_var but then it prints "Invalid link"

acw1668
  • 40,144
  • 5
  • 22
  • 34
  • Does this answer your question? [How to pass arguments to a Button command in Tkinter?](https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter) – TZHX May 07 '23 at 07:12
  • pytube has a bug, see [pytube](https://github.com/pytube/pytube/issues) – Hermann12 May 07 '23 at 08:28
  • Does this link help? https://stackoverflow.com/questions/75255584/cant-update-label-in-realtime-in-customtkinter-even-with-the-thread – toyota Supra May 08 '23 at 01:23

1 Answers1

0

the problem was that you didn't pass the URL in the button command. But with this, you don't need to. I also added error handling because some videos are age-restricted, and if you just print out an invalid link, you will never know why.

`import tkinter as tk
import customtkinter as ctk
from pytube import YouTube

def download_video():
    try:
        yt_link = url_var.get()
        yt_object = YouTube(yt_link)
        video = yt_object.streams.get_highest_resolution()
        video.download()
        print("Download Complete")
    except Exception as e:
        print(f"Invalid link{e}")

ctk.set_appearance_mode("System")
ctk.set_default_color_theme("blue")

app = ctk.CTk()
app.geometry("720x480")
app.title("YouTube Download")

url_var = tk.StringVar()
title = ctk.CTkLabel(app, text="Insert a YouTube link")
title.pack(padx=10, pady=10)

link = ctk.CTkEntry(app, width=350, height=40, textvariable=url_var)
link.pack()

download = ctk.CTkButton(app, text="Download", command=download_video)
download.pack()

app.mainloop()
`
pyroarti
  • 1
  • 1
  • And btw for future, the way to pass a arg into a command is with a lambda like this: command= lambda: Download(url_var)) – pyroarti May 11 '23 at 06:43