我有 python 个带有 pytube 和 tkinter 的应用程序

I have python application with pytube and tkinter

我制作了一个应用程序,您可以在其中下载 youtube 视频并选择下载位置。问题是,它只是有时有效。

这是它在控制台中给出的错误:Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__ return self.func(*args) File "C:\Python\VideoDownloader\VideoDownloader.py", line 39, in downloadVideo ytbvideo=YouTube(ytbLink).streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 91, in __init__ self.prefetch() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 183, in prefetch self.js_url = extract.js_url(self.watch_html) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\extract.py", line 143, in js_url base_js = get_ytplayer_config(html)["assets"]["js"] KeyError: 'assets'

这是我的代码:

import os
from tkinter import *
from tkinter import filedialog
import tkinter as tk



root= Tk()
root.geometry('600x400')
root.title('Youtube Video Downloader')
root.configure(bg='gray')



Label_1=Label(root,text="Youtube video downloader", font=("bold",20), bg='gray')
Label_1.place(x=150,y=10)

Label_2=Label(root, text="Paste the link here", font=(10), bg='gray')
Label_2.place(x=240, y=75)



mylink=StringVar()

pastelink=Entry(root, width=60, textvariable=mylink)
pastelink.place(x=140, y=100)



def chooseDir():
    global path
    path = filedialog.askdirectory(title="Choose a download directory")
    videoLoc = path
    tk.Label(root, text=path, bg='gray').place(x=240,y=300)

def downloadVideo():
    ytbLink=str(mylink.get())
    ytbvideo=YouTube(ytbLink).streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
    ytbvideo.download(videoLoc)


    
def quitApp():
    root.destroy()
    



   

Button(root,text="Download video", width=20, bg='black',fg="white", command=downloadVideo).place(x=240, y=130)
Button(root,text="Choose location", width=20, bg='black',fg="white", command=chooseDir).place(x=240, y=160)
Label_3=Label(root, text="Curent location: ", font=("bold"), bg='gray')
Label_3.place(x=250, y=245)

Label_3=Label(root, text="by yakubiq", font=("bold"), bg='gray')
Label_3.place(x=0, y=375)

Button(root,text="Quit", width=20, bg='black', fg='white', command=quitApp).place(x=445, y=370)



root.mainloop()

我试图自己修复它,但我仍然是初学者,几天前我就开始了。在控制台中,有第 183 行,但我不知道它是什么意思

此问题已在较新版本的 pytube 上修复,只需尝试说 pip uninstall pytube 然后 pip install pytube 然后重新 运行 您的代码。

就您的 tkinter 代码而言,您将 global 分配给了错误的变量。

def chooseDir():
    global videoLoc #correct globalization
    path = filedialog.askdirectory(title="Choose a download directory")
    videoLoc = path
    tk.Label(root, text=path, bg='gray').place(x=240,y=300)

def downloadVideo():
    ytbLink = mylink.get() #no need to use str() as it is string by default 
    ytbvideo = YouTube(ytbLink).streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
    ytbvideo.download(videoLoc)

通过这些更改它应该可以工作,但请记住,在您给出的示例中,您没有导入 pytube,因此它必须像 from pytube import YouTube。虽然我也建议你使用 threading.