我仍在开发我的小型 Tkinter 项目,这是一个简单的 YouTube 视频下载器,每次我尝试使用 tkinter 窗口时,它都会给我一个异常回调和值错误
Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "file.py", line 7, in downloadVid yt = YouTube(str(string)) File "C:\Users\shaswat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pytube\__main__.py", line 92, in __init__ self.descramble() File "C:\Users\shaswat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pytube\__main__.py", line 140, in descramble apply_signature(self.player_config_args, fmt, self.js) File "C:\Users\shaswat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pytube\extract.py", line 225, in apply_signature cipher = Cipher(js=js) File "C:\Users\shaswat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pytube\cipher.py", line 31, in __init__ var, _ = self.transform_plan[0].split(".") ValueError: too many values to unpack (expected 2)
以下是代码
import tkinter as tk from pytube import YouTube def downloadVid(): global E1 string =E1.get() yt = YouTube(str(string)) videos = yt.get_videos() s=1 for v in videos: print(str(s) + '.' + str(v)) s +=1 n=int(input("Enter your choice")) vid=videos[n-1] destination=str(input("Enter your destination")) vid.download(destination) print(yt.filename+"\n Ha been downloaded") root=tk.Tk() w=tk.Label(root,text="Youtube Downloader") w.pack() E1=tk.Entry(root,bd=5) E1.pack(side=tk.TOP) button=tk.Button(root,text="Download",fg="red",command=downloadVid ) button.pack(side=tk.BOTTOM) root.mainloop()
你的代码的问题主要是由于 pytube 的版本和 API 变化导致的。以下是问题的来源和解决方案:
pytube
ValueError: too many values to unpack
这是 pytube 的常见问题,因为 YouTube 不断更新其网站,导致 pytube 的解析代码失效。
旧 API 使用问题:
get_videos()
yt.filename
streams
以下是更新后的代码,修复了上述问题并使用了正确的 pytube 方法:
import tkinter as tk from tkinter import filedialog, messagebox from pytube import YouTube def download_vid(): url = entry.get() if not url.strip(): messagebox.showerror("Error", "Please enter a valid URL!") return try: yt = YouTube(url) # 获取最高质量的视频流 video_stream = yt.streams.get_highest_resolution() # 选择保存路径 destination = filedialog.askdirectory() if not destination: return # 用户取消了保存路径选择 # 下载视频 video_stream.download(output_path=destination) messagebox.showinfo("Success", f"'{yt.title}' has been downloaded successfully!") except Exception as e: messagebox.showerror("Error", f"An error occurred: {e}") # 创建主窗口 root = tk.Tk() root.title("YouTube Video Downloader") # 标签 label = tk.Label(root, text="YouTube Video URL:", font=("Arial", 12)) label.pack(pady=10) # 输入框 entry = tk.Entry(root, width=40, font=("Arial", 12)) entry.pack(pady=10) # 按钮 download_button = tk.Button(root, text="Download Video", command=download_vid, bg="red", fg="white", font=("Arial", 12)) download_button.pack(pady=20) root.mainloop()
yt.streams.get_highest_resolution()
也可以通过筛选条件选择音频流或特定分辨率的流。
选择下载路径:
使用 filedialog.askdirectory() 让用户选择保存路径。
filedialog.askdirectory()
用户反馈:
用 messagebox 提供错误提示或成功消息。
messagebox
错误处理:
更新 pytube 以避免因 YouTube 网站更改而出现的问题: bash pip install --upgrade pytube
bash pip install --upgrade pytube
替代工具:
yt-dlp
希望这些改动能解决你的问题!如果仍有疑问,请随时提出。