问题描述
我正在尝试使用 python 模块 pytube 执行 YouTube 下载程序,但遇到此错误.
I was trying to do a YouTube downloader program, using the python module pytube and i encountered this error.
TypeError: 不支持的操作数类型 -: 'int' 和 'NoneType'
我试图在开始下载按钮上显示下载的百分比.(我正在使用 tkinter)
I was trying to display the percentage downloaded on the start download button. (i am using tkinter)
这是我的进度函数代码:
this is my progress function code:
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())
我尝试将剩余=无更改为剩余,但没有用
i tried changing that remaining=None to remaining, but didnt work
这是我写的全部代码
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()
如果有人可以帮助我编写代码,这将非常有帮助.:)
It'll be really helpful, if somebody could help me with the code. :)
推荐答案
回调函数需要三个参数:stream
、chunk
和 remaining
.另外 on_progress_callback=progress_function()
应该是 on_progress_callback=progress_function
.
The callback function expects three arguments: stream
, chunk
and remaining
. Also on_progress_callback=progress_function()
should be on_progress_callback=progress_function
instead.
这是我问题的解决方案....非常感谢@acw1668
This is the solution to my problem....Thank you soo much @acw1668
这篇关于python模块pytube的on_progress_callback问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!