python - python模块pytube的on_progress_callback问题
问题描述
我试图做一个 YouTube 下载程序,使用 python 模块 pytube,我遇到了这个错误。
TypeError: 不支持的操作数类型 -: 'int' 和 'NoneType'
我试图在开始下载按钮上显示下载的百分比。(我正在使用 tkinter)
这是我的进度功能代码:
def progress_function(stream=None, chunk=None, file_handle=None, remaining=None):
file_downloaded=(file_size-remaining)
per = (file_downloaded/file_size)*100
dBtn.config(text="{} % Downloaded".format(per))
我在这里叫它
ob = YouTube(url, on_progress_callback=progress_function())
我尝试将剩余 = 无更改为剩余,但没有工作
这是我写的全部代码
from pytube import *
from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *
from threading import *
from PIL import ImageTk,Image
file_size = 0
def progress_function(stream=None, chunk=None, file_handle=None, remaining=None):
file_downloaded=(file_size-remaining)
per = (file_downloaded/file_size)*100
dBtn.config(text="{} % Downloaded".format(per))
def startDownload():
global file_size
#changing Button text
url = urlField.get()
dBtn.config(text='Please wait...')
dBtn.config(state=DISABLED)
path_to_save = askdirectory()
if path_to_save is None:
return
ob = YouTube(url, on_progress_callback=progress_function())
stream_list = ob.streams.first()
file_size = stream_list.filesize
stream_list.download(path_to_save)
print("Done...")
dBtn.config(text="Start Download")
dBtn.config(state=NORMAL)
showinfo("Donwload Completed", "Downloaded Successfully")
def startDownloadThread():
thread=Thread(target=startDownload)
thread.start()
# starting gui building
main = Tk()
# setting the title
main.title("Youtube Downloader!!!")
main.geometry("500x600")
#heading image
path = "youtube.png"
img= ImageTk.PhotoImage(Image.open(path))
panel = Label(main, image=img)
panel.pack(side="top", fill="both", expand="no")
#url text field
urlField=Entry(main, font=("verdana", 18), justify=CENTER)
urlField.pack(side=TOP, fill=X, padx=20)
#download button
dBtn = Button(main, text="Start Download", font=("verdana", 18), relief='ridge', command=lambda : startDownloadThread())
dBtn.pack(side=TOP, pady=20)
main.mainloop()
如果有人可以帮助我编写代码,那将非常有帮助。:)
解决方案
回调函数需要三个参数stream
:chunk
和remaining
。也on_progress_callback=progress_function()
应该on_progress_callback=progress_function
改为。
这是我的问题的解决方案....非常感谢@acw1668
推荐阅读
- xhtml - 主体不跨越视口的宽度
- azure - ADSTS50011:回复地址与为应用程序配置的回复地址不匹配
- unix - 如何在 WSL 上使用 dd?
- perl - Cwd.c:可加载库和 perl 二进制文件不匹配(获取握手密钥 0xdb00080,需要 0xde00080)
- ionic-framework - Ionic 3 本机地理围栏插件无法在 xcode 9 上运行,因为地理围栏插件不支持 swift 3
- html - how to define two pipes in the same component
- javascript - Highlighting with php generated checkbox form
- vbscript - 自动填充表单 IE VBScripting
- .net-core - 为什么 NLog 会忽略项目目录中的 NLog.config 文件?
- javascript - Remove last option value from multiple select element using jQuery