python模块pytube的on_progress_callback问题

问题描述

我正在尝试使用python模块pytube制作YouTube下载程序,但是遇到了此错误

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,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",relief='ridge',command=lambda : startDownloadThread())
dBtn.pack(side=TOP,pady=20)

main.mainloop()

如果有人可以帮助我提供代码,那将真的很有帮助。 :)

解决方法

回调函数需要三个参数:streamchunkremaining。另外,on_progress_callback=progress_function()应该改为on_progress_callback=progress_function

这是解决我的问题的方法。 非常感谢@ acw1668

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...