【问题标题】:Problem with on_progress_callback of python module pytubepython模块pytube的on_progress_callback问题
【发布时间】:2021-02-01 15:01:55
【问题描述】:

我正在尝试使用 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, 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()

如果有人可以帮助我编写代码,那将非常有帮助。 :)

【问题讨论】:

  • 错误是因为在progress_function()的参数中你说remaining=None,所以它变成NoneType,因此你不能做file_size-remaining,因为一个是int,另一个是None ,因此请尝试删除 remaining=None
  • yepp... 我试过这样做,但它不起作用。我从所有内容中都没有删除,但仍然无法正常工作
  • 这是唯一应该出现错误的地方。否则,如果未定义,可能会变成None 我不确定 pytube 是如何工作的,但它必须是整数。
  • 当我删除 None 时,它​​会给出另一个错误,例如 `progress_function() missing 1 required positional argument: 'remaining' `
  • 回调函数需要三个参数:streamchunkremaining。另外on_progress_callback=progress_function() 应该是on_progress_callback=progress_function

标签: python tkinter progress-bar pytube


【解决方案1】:

回调函数需要三个参数:streamchunkremainingon_progress_callback=progress_function() 也应该是 on_progress_callback=progress_function

这是我的问题的解决方案.... 非常感谢@acw1668

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多