无法使用asksaveas功能

问题描述

我正在制作一个GUI应用程序,用户可以从youtube下载任何视频,以下是我正在使用的代码

from pytube import YouTube
from tkinter import *
from tkinter import message@R_404_6277@
from tkinter.filedialog import *
from threading import *

root=Tk()

root.iconbitmap('yticon.ico')
root.title('Youtube Video Downloader')
root.resizable(0,0)

file_size=0
url=''

def downThread():
    p1=Thread(target=downloader)
    p1.start()
def progress(chunk,file_handle,remaining):
    global download_status
    file_download=file_size-remaining
    per=(file_download/file_size)*100
    download_status.config(text='{:00.0f}% downloaded'.format(per))

def downloader():
    global file_size,download_status,url
    download_btn.config(state=disABLED)
    download_status.place(x=200,y=250)
    #try:
    link=url.get()
    path=asksaveasfilename(title='Save File',defaultextension='.mp4',filetype=(('*.mp4','MP4 Files')))
    yt=YouTube(link,on_progress_callback=progress)
    video=yt.streams.filter(progressive=True,file_extension='mp4').first()
    file_size=video.filesize
    #assert isinstance(video,bytes)
    video.download(path)
    download_status.config(text='Download Finished...')
    res=message@R_404[email protected]('Youtube Downloader','Do You want to download another video?')
    if res==1:
        url.delete(0,END)
        download_btn.config(state=norMAL)
        download_status.config(text=' ')
    else:
        root.destroy()
    #except Exception as e:
     #   download_status.config(text='Failed!!There was an error')

imgfile=PhotoImage(file='yticon1.png')
back_img=Label(root,image=imgfile)
back_img.place(x=0,y=0,relwidth=1,relheight=1)
entr_url=Label(root,text='Enter Url',font='Algerian 15 bold')
entr_url.place(x=20,y=40)
url=Entry(root,width=30,bd=2,relief=SUNKEN,font=('Calibiri(Body)',12))
url.place(x=140,y=40)
btnimg=PhotoImage(file='yt.png')
download_btn=Button(root,width=99,height=50,relief=RIDGE,activebackground='red',image=btnimg,command=downThread,bd=7)
download_btn.place(x=220,y=170)
download_status=Label(root,text='Please Wait...',font=('Arial',15))
root.geometry('700x400+150+150')
root.mainloop()

但是当我运行这段代码时,它给出了此错误,我该如何解决呢?

_tkinter.TclError: bad file type ".mp4",should be "typeName {extension ?extensions ...?} ?{macType ?macTypes ...?}?"

解决方法

link = url.get()

path=asksaveasfilename(title='Save File',defaultextension='.mp4',filetype=(('*.mp4','MP4 Files')))

yt = YouTube(链接,on_progress_callback =进度) [('* .mp4','mp4 File')])` 您必须执行此操作,因为文件类型将采用列表形式。