基于 tkinter GUI 的 Reddit Scraper 应用程序中的 NameError --Python

NameError in tkinter GUI based Reddit Scraper Application --Python

我正在构建一个基于 GUI 的 Reddit 爬虫应用程序,我遇到了 运行 一些问题。首先,我似乎无法从 redditReturn class 文件中加载我的第二个 tkinter window。此外,我不确定让我的代码部分 运行 是 Reddit API 以及 运行 构造 tkinter windows 的方法是否正确。唉,我主要关心的是如何纠正错误:

Traceback (most recent call last):
  File "redditscraper4.py", line 77, in <module>
    app = RedditScraper()
  File "redditscraper4.py", line 21, in __init__
    frame = F(container, self)
  File "redditscraper4.py", line 62, in __init__
    newFrame = LabelFrame(self, text = intro)
NameError: name 'LabelFrame' is not defined

我现在还不确定如何解决这个问题。感谢您的帮助!

完整代码:

import tkinter as tk
import praw



class RedditScraper(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, redditReturn):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Start Page")
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self, text="Scrape This Subreddit", command=lambda: controller.show_frame(redditReturn))
        button1.pack(pady=10, padx=10)

        e1 = tk.Entry(self)
        e1.pack(pady=10, padx=10)

        subreddit_Chosen = '"' + e1.get() + '"'

class redditReturn(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)


        user_agent = "Simple Subreddit Scraper"
        r = praw.Reddit(user_agent=user_agent)
        posts = r.get_subreddit("pics").get_hot(limit = 10)


        """Creates all the buttons and frames for the GUI"""
        intro = "News on Reddit: "
        newFrame = LabelFrame(self, text = intro)
        newFrame.pack(fill="both", expand="yes", anchor = NW)        
        row = 0
        for p in posts:
            gotoArticle = partial(open, p.url)
            title = "(" + str(p.score) +") " + p.title
            Label(newFrame, text= title, pady= 10, wraplength= 700, justify= LEFT).grid(row= row, column= 0, sticky= W)
            Button(newFrame, text= "Read more!", command= gotoArticle).grid(row= row+1, column= 0, sticky= W)
            row = row + 2

        self.pack()
        self.redditReturn()



app = RedditScraper()
app.mainloop()

此外,作为旁注。如果有人知道如何更改我正在从行中抓取的特定 subreddit:

posts = r.get_subreddit("pics").get_hot(limit = 10)

我似乎一辈子都无法连接上一个条目 tkinter 元素,以将 "pics" 更改为这行代码中的其他内容。

再次感谢!

这个错误告诉你到底是什么问题。您正在调用名为 LabelFrame 的函数或 class,但不存在这样的函数或 class。所以真正的问题是,"why does it not exist?"

因为您是这样导入 Tkinter 的:

import Tkinter as tk

...您需要在所有 tkinter classes 和命令前加上 tk. 前缀。由于 LabelFrame 是一个 tkinter 小部件,您需要像这样创建它:

newFrame = tk.LabelFrame(self, text = intro)