当前位置: 首页 > 知识库问答 >
问题:

带有GUI的Python Youtube下载程序

督飞羽
2023-03-14

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

根据我的理解,当我们使用“*”时,会导入库中存在的所有模块。再一次为什么我们必须使用"from tkinter导入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个”。

你建议利用两个班。因为我还是初学者,所以我对“自我”的争论有点困惑。所以我用了单节课。

你能告诉我哪里出了问题吗。

非常感谢。

共有1个答案

皇甫福
2023-03-14

所有语法错误都已解决。只有一个例外,我认为你可以解决。

使用类时,请使用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()

 类似资料:
  • 问题内容: 我需要从Docker容器中在后台运行的Java应用程序中启动Selenium。启动失败,因为在运行时无法访问X11环境。请参阅下面的内容。 我该怎么办? 问题 我从安装Java 8和Jetty 9.3.x 的简单程序开始运行一个简单的服务(实际上是selenium的东西)。该服务实际上是为了启动一些需要UI才能执行的事情而设置的。我遇到的问题是其中的任何内容执行失败,因为UI在我运行的

  • 本文向大家介绍python 批量下载bilibili视频的gui程序,包括了python 批量下载bilibili视频的gui程序的使用技巧和注意事项,需要的朋友参考一下 运行效果: 完整代码: 以上就是python 批量下载bilibili视频的gui程序的详细内容,更多关于python 批量下载bilibili视频的资料请关注呐喊教程其它相关文章!

  • 我正在尝试加强我对asyncTask的了解,所以我按照教程下载了一个图像,但我不明白,我得到了一个错误的ArrayOutofBound索引,所以如果有人告诉我原因这里是代码,请告诉我为什么他迭代进度条最大 下面是堆栈跟踪01-20 03:37:40.607 407 5-4096/com.gamecodeschool.bodybuilder E/AndroidRuntime:致命异常:AsyncTa

  • 我在我的应用程序中使用了XSD中的gml(3.1.1)XSDs。我想下载版本3.1.1中的所有gml XSD,例如zip文件。换句话说:基本xsd在这里,我想下载这个xsd,所有的导入都在zip文件或类似zip文件的东西中。是否有任何应用程序支持这一点?我找到了这个下载器,但它对我不起作用(我认为这个应用程序不支持gml.xsd3.1.1中出现的导入中的相对路径)。有什么想法吗?

  • 我有一个证书文件“cert. p12”和这个证书的密码“yxc123yxc”。有了这个,我可以对网站进行身份验证,下载一个xml文件。 我如何在php中做到这一点?尝试过这样做,但“$响应”是错误的。