gpt4 book ai didi

带有 GUI 的 Python Youtube 下载器

转载 作者:行者123 更新时间:2023-12-03 06:16:09 26 4
gpt4 key购买 nike

我正在尝试使用 python3 构建带有 GUI 的 youtube 下载器应用程序。
在学习了python的基础知识之后,我正在尝试构建一个。我正在使用“pafy”和“TKinter”模块。

以下是 GUI 中涉及的小部件
1. 一栏输入网址
2.在它旁边粘贴按钮
3. 媒体选择下拉菜单
4.多一个下拉菜单列出媒体质量(取决于之前的媒体输入)
5.最后一个下载按钮

这是我的代码

from tkinter import *
from tkinter import ttk
import pafy

master = Tk()

media_option = StringVar()

audio_quality_lsit = []
audio_quality_drop_menu = StringVar()

video_quality_lsit = []
video_quality_drop_menu = StringVar()


def url():
global data
url = url_field.get()
data = pafy.new(url)


def audio():
global selected_audio
audio_streams = data.audiostreams
for audio_quality in audio_streams:
audio_quality_lsit.append(audio_quality.bitrate)
selected_audio = audio_quality_drop_menu.get()
print("selected_audio") #Debug Statement
Label(master, text="Audio Bitrate: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(master, audio_quality_drop_menu, *audio_quality_lsit)
show_drop_menu.grid(row=9, column=1)



def video():
global selected_video
video_streams = data.streams
for video_quality in video_streams:
video_quality_lsit.append(video_quality.resolution)
selected_video = video_quality_drop_menu.get()
print("selected_video") #Debug Statement
Label(master, text="Video Quality: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(master, video_quality_drop_menu, *video_quality_lsit)
show_drop_menu.grid(row=9, column=1)


def media_select(self):
global A_V
A_V = media_option.get()
if A_V == "Audio":
audio()
elif A_V == "Video":
video()


def download():
if selected_audio:
print("Audio") #Debug Statement
selected_audio.download(quiet=False)
elif selected_video:
print("Video") #Debug Statement
selected_video.download(quiet=False)


Label(master, text="YouTube URL:").grid(row=0)
url_field = Entry(master)
url_field.grid(row=0, column=1)

Button(master, text='Paste URL', command=url).grid(row=0, column=4, sticky=W, pady=10)
Button(master, text='Download', command=download).grid(row=12, column=4, sticky=W, pady=10)
Label(master, text="Media Type: ").grid(row=3)

media_drop_menu = OptionMenu(master, media_option, "Audio", "Video", command=media_select)
media_drop_menu.grid(row=5, column=0)

mainloop()

我被困在“下载功能”上。我是否正确使用该功能?
调用函数有问题吗?
还有一件事,对不起,如果是转储问题。
为什么我们必须这样做,
from tkinter import *
from tkinter import ttk

根据我的理解,当我们使用“*”时,将导入库中存在的所有模块。再次说明为什么我们必须使用“from tkinter import ttk”。

请帮我。

更新:
@JoshuaNixon 感谢重播。
我对“类”知之甚少,所以首先我了解了它的基础知识并进行了一些更改。

这是代码:
from tkinter import *
import pafy

class YouTubeDownloader:
def __init__(self,master):
self.master = master
master.title("Youtube Downloader")
self.media_option = StringVar()
self.audio_quality_lsit = []
self.audio_quality_drop_menu = StringVar()
self.video_quality_lsit = []
self.video_quality_drop_menu = StringVar()
Label(master, text="YouTube URL:").grid(row=0)
self.url_field = Entry(master)
self.url_field.grid(row=0, column=1)
Button(master, text='Paste URL', command=self.url).grid(row=0, column=4, sticky=W, pady=10)
Button(master, text='Download', command=self.download).grid(row=12, column=4, sticky=W, pady=10)
Label(master, text="Media Type: ").grid(row=3)
media_drop_menu = OptionMenu(master, self.media_option, "Audio", "Video", command=self.media_select)
media_drop_menu.grid(row=5, column=0)


def url(self):
global data
url = self.url_field.get()
data = pafy.new(url)


def media_select(self):
global A_V
A_V = self.media_option.get()
if A_V == "Audio":
audio()
elif A_V == "Video":
video()


def audio():
global selected_audio
audio_streams = self.data.audiostreams
for audio_quality in audio_streams:
audio_quality_lsit.append(audio_quality.bitrate)
selected_audio = audio_quality_drop_menu.get()
print("selected_audio") #Debug Statement
Label(master, text="Audio Bitrate: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(master, audio_quality_drop_menu, *audio_quality_lsit)
show_drop_menu.grid(row=9, column=1)


def video(self):
global selected_video
video_streams = self.data.streams
for video_quality in video_streams:
video_quality_lsit.append(video_quality.resolution)
selected_video = video_quality_drop_menu.get()
print("selected_video") #Debug Statement
Label(master, text="Video Quality: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(master, video_quality_drop_menu, *video_quality_lsit)
show_drop_menu.grid(row=9, column=1)


def download(self):
if selected_audio:
print("Audio") #Debug Statement
self.selected_audio.download(quiet=False)
elif selected_video:
print("Video") #Debug Statement
self.selected_video.download(quiet=False)



root = Tk()
my_youtube = YouTubeDownloader(root)
root.mainloop()

当我运行代码时,我得到了 GUI。复制粘贴 URL 字段后,我在“media_select()”处收到错误消息

“TypeError:media_select() 采用 1 个位置参数,但给出了 2 个”。

你建议使用两个类。由于我还是初学者,因此我对“自我”论点感到困惑。所以我使用了单节课。

你能告诉我哪里出错了。

谢谢你。

最佳答案

所有语法错误都已解决。只有一个异常(exception),我认为你可以解决。

当你使用一个类时,使用 self.*供引用,

from Tkinter import *
import pafy

class YouTubeDownloader:
def __init__(self,master):
self.master = master
master.title("Youtube Downloader")
self.media_option = StringVar()
self.audio_quality_lsit = []
self.audio_quality_drop_menu = StringVar()
self.video_quality_lsit = []
self.video_quality_drop_menu = StringVar()
Label(master, text="YouTube URL:").grid(row=0)
self.url_field = Entry(master)
self.url_field.grid(row=0, column=1)
Button(master, text='Paste URL', command=self.url).grid(row=0, column=4, sticky=W, pady=10)
Button(master, text='Download', command=self.download).grid(row=12, column=4, sticky=W, pady=10)
Label(master, text="Media Type: ").grid(row=3)
media_drop_menu = OptionMenu(master, self.media_option, "Audio", "Video", command=self.media_select)
media_drop_menu.grid(row=5, column=0)
self.data = []

def url(self):
global data
url = self.url_field.get()
self.data = pafy.new(url)

def media_select(self, a):
global A_V
A_V = self.media_option.get()
if A_V == "Audio":
# for functions of same class use self.function()
self.audio()
elif A_V == "Video":
self.video()

def audio(self):
global selected_audio
audio_streams = self.data.audiostreams
for audio_quality in audio_streams:
self.audio_quality_lsit.append(audio_quality.bitrate)
selected_audio = self.audio_quality_drop_menu.get()
print("selected_audio") #Debug Statement
Label(self.master, text="Audio Bitrate: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(self.master, self.audio_quality_drop_menu, *self.audio_quality_lsit)
show_drop_menu.grid(row=9, column=1)

def video(self):
global selected_video
video_streams = self.data.streams
for video_quality in video_streams:
self.video_quality_lsit.append(video_quality.resolution)
selected_video = self.video_quality_drop_menu.get()
print("selected_video") #Debug Statement
Label(self.master, text="Video Quality: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(self.master, self.video_quality_drop_menu, *self.video_quality_lsit)
show_drop_menu.grid(row=9, column=1)

def download(self):
if selected_audio:
print("Audio") #Debug Statement
self.selected_audio.download(quiet=False)
elif selected_video:
print("Video") #Debug Statement
self.selected_video.download(quiet=False)

root = Tk()
my_youtube = YouTubeDownloader(root)
root.mainloop()

关于带有 GUI 的 Python Youtube 下载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43697511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com