Tkinter程序,播放列表Scraper

时间:2017-09-04 23:48:58

标签: python tkinter

此程序旨在从youtube播放列表中删除所有链接

# coding=utf-8


from tkinter import * 
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

class YoutubeScraper:

    def __init__(self,master):
        label = Label(master, text='Youtube Playlist scraper!')
        label.pack()
        frame = Frame(master)
        frame.pack()


        self.runButton = Button(frame, text='Print Message', command=runProgram)
        self.runButton.pack(side=LEFT)

        self.quitButton = Button(frame, text='QUIT', command=frame.quit)
        self.quitButton.pack(side=LEFT)

    def runProgram(self):
        print("This works")

        # my_url = 'https://www.youtube.com/watch?v=P8bx4nits-o&index=5&list=PL6gx4Cwl9DGAjkwJocj7vlc_mFU-4wXJq'

        # uClient = uReq(my_url)
        # page_html = uClient.read()
        # uClient.close()

        # # Cleaning up the HTML
        # page_soup = soup(page_html, 'html.parser')

        # # Grabs each product!
        # containers = page_soup.findAll('li', {'class': 'yt-uix-scroller-scroll-unit'})

        # # writes data to a file!
        # fileName = 'Pygame.txt'
        # f = open(fileName, 'w')

        # for container in containers:
        #     title = container.a.h4.string
        #     link = container.a['href']
        #     href = 'https://youtube.com/' + link
        #     print(title)
        #     print(href)

        #     f.write(title + " " + href + '\n\n')
        # f.close()

root = Tk()
program_run = YoutubeScraper(root)
root.mainloop()

我需要帮助让这个工作,我仍然试图将我的大脑包裹在函数周围,我得到一个错误,我的函数runProgram没有定义! 我不明白bc即使使用pass作为函数定义它仍然给我一个错误,它没有被定义!

1 个答案:

答案 0 :(得分:1)

就像你需要使用'self'来引用类的其他函数中的属性...例如使用self.runButton而不仅仅是runButton,你需要使用self来引用类中的函数。

self.runButton = Button(frame, text='Print Message', command=self.runProgram)