我正在尝试使用python3使用GUI构建youtube下载程序。 在学习了python的基础知识后,我正在尝试构建一个。我正在使用“pafy”和“TKinter”模块。
以下是GUI中涉及的小部件 1.输入URL的一个输入字段 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
根据我的理解,当我们使用“*”时,将导入库中存在的所有模块。再次为什么我们必须使用“来自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个被赋予”。
您建议使用两个类。由于我还是初学者,我对“自我”论证感到困惑。所以我用了单课。
你能告诉我我哪里出错了。谢谢。
答案 0 :(得分:0)
所有语法错误均已解决。我认为您只能解决一个例外。
使用类时,请使用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()