Search code examples
pythontkintertypeerrorredditscraper

Tkinter Reddit scraper (Tkinter and Praw) returns error "TypeError"


Im relatively new to python and am trying to create a tkinter application that collects information from a specific subreddit and returns it back to the user in a tkinter window. However, when I run the code it returns the error:

TypeError: 'NoneType' object has no attribute 'getitem'

There appears to be nothing else on the web that can fix this, therefore I believe it's appropriate to be asked here.

My apologies if there are many other problems with my code here, remember, I am very new to coding.

Thanks, -Jeff

try:
from Tkinter import *
except ImportError:
from tkinter import *
from webbrowser import open
from datetime import date
import praw

"""
This scraper will (eventually be able to) search a user-defined subreddit and return the top ten posts from that subreddit"
"""

class redditScraper(Frame):

    def makeWidgets(self):
        intro = "Reddit Client"

        Label(self, text="What Subreddit do you wish to view?").pack()
        self.e = Entry(self)
        self.e.pack(padx=5)

        b = Button(self, text="Search Subreddit", command=self.search)
        b.pack(pady=5)


    def search(self):
        user_agent = "Reddit Client (of a sort), by (/u/CowInAFridge)"
        r = praw.Reddit(user_agent=user_agent)
        posts = r.get_subreddit(self.e.get()).get_hot(limit = 10)
        self.makeWidgets.distroy
        return posts

    def actualFrame(self):  

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

    def __init__(self, master):  
        Frame.__init__(self, master)        
        self.makeWidgets()
        self.actualFrame()
        self.pack()

    root = Tk()
    app = redditScraper(root)
    app.master.title("Reddit Client V.1.0")
    app.mainloop()
    root.distroy

The error that gets outputted is as follows:

Traceback (most recent call last):
  File "myRedditScraper.py", line 53, in <module>
    app = redditScraper(root)
  File "myRedditScraper.py", line 49, in __init__
    self.actualFrame()
  File "myRedditScraper.py", line 37, in actualFrame
    posts = self.search()
  File "myRedditScraper.py", line 29, in search
    posts = r.get_subreddit(self.e.get()).get_hot(limit = 10)
  File "/Library/Python/2.7/site-packages/praw/__init__.py", line 1018, in get_subreddit
    return objects.Subreddit(self, subreddit_name, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/praw/objects.py", line 1356, in __init__
    subreddit_name = json_dict['url'].split('/')[2]
TypeError: 'NoneType' object has no attribute '__getitem__'

Solution

  • I think the issue here is that you're attempting to get the value in Entry self.e before you call app.mainloop().

    In self.actualFrame() you call self.search(), which makes this call:

    posts = r.get_subreddit(self.e.get()).get_hot(limit = 10)
    

    Before you attempt to call self.e.get() you must start the mainloop of the GUI.

    I'm not completely clear on the structure of your code, but if you are trying to retrieve a value from self.e, wait to call self.search() until after you call app.mainloop()