Search code examples
pythonuser-interfacetkinterredditscraper

NameError in tkinter GUI based Reddit Scraper Application --Python


I'm in the process of building a GUI based Reddit scraper application and I have run into a few problems. First, I can't seem to get my second tkinter window to load from the redditReturn class file. Also, i'm not sure if it is correct to have my section of code that runs the Reddit API alongside methods that are being run to construct tkinter windows. Alas, my main concern is how to rectify the Error:

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

I'm not sure how to rectify the problem as of now. Any help is appreciated!

Full Code:

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()

Also, as a side-note. If anyone knows how to change the specific subreddit i'm scraping from in the line:

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

I can't seem to for the life of my be able to connect a previous entry tkinter element to change the "pics" to something else in this line of code.

Again, Thanks!


Solution

  • The error is telling you exactly what the problem is. You are calling a function or class named LabelFrame, but no such function or class exists. So the real question is, "why does it not exist?"

    Because you are importing Tkinter like this:

    import Tkinter as tk
    

    ... you need to prefix all tkinter classes and commands with tk.. Since LabelFrame is a tkinter widget, you need to create it like this:

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