我可以下载视频,但它没有显示字符串“下载完成”,视频也没有出现在我选择的路径中,而是出现在我保存代码的文件夹中。此外,控制台显示错误,下载视频需要一段时间。
目的很简单,你粘贴一个YouTube视频的链接,然后选择一个你想要下载视频的本地路径。
from tkinter import *
from tkinter import filedialog
from moviepy import *
from moviepy.editor import VideoFileClip
from pytube import YouTube
import shutil
screen = Tk()
title = screen.title("Youtube Downloader")
canvas = Canvas(screen, width=500, height=500)
canvas.pack()
def select_path():
path = filedialog.askdirectory()
path_label.config()
def download_video():
get_link = link_field.get()
user_path = path_label.cget("text")
screen.title('Downloading... Please wait...')
mp4_video = YouTube(get_link).streams.get_highest_resolution().download()
vid_clip = VideoFileClip(mp4_video)
vid_clip.close()
shutil.move(mp4_video, user_path)
screen.title('Download Completed!')
logoimg = PhotoImage(file="images/download.png")
logoimg = logoimg.subsample(2, 2)
canvas.create_image(250, 80, image=logoimg)
link_field = Entry(screen, width=50)
link_label = Label(screen, text="Paste Download Link: ", font=("Sans Serif", 13))
canvas.create_window(250, 170, window=link_label)
canvas.create_window(250, 210, window=link_field)
path_label = Label(screen, text="Select Local Path: ", font=("Sans Serif", 13))
select_button = Button(screen, text="Select", command=select_path) # Select Button
canvas.create_window(250, 270, window=path_label)
canvas.create_window(250, 320, window=select_button)
download_bttn = Button(screen, text="Download Video", font=("Sans Serif", 12), command=download_video)
canvas.create_window(250, 400, window=download_bttn)
screen.mainloop()
1条答案
按热度按时间e37o9pze1#
看起来你实际上并没有更新你的
path_label
文本。你在select_path()
中调用path_label.config()
,但是你没有传递任何东西给config
调用。我假设你想要path_label.config(text=path)
。StringVar()
绑定到你的path_label
的textvariable
上。这样,每当StringVar()
的值改变时(通过调用StringVar.set()
),标签就会自动更新。你也可以很容易地用StringVar.get()
检索值