我是python的新手,我正在尝试创建一个tkinter应用程序,它从特定的subreddit收集信息并在tkinter窗口中将其返回给用户。但是,当我运行代码时,它返回错误:
TypeError:' NoneType'对象没有属性' getitem '
网上似乎没有任何其他内容可以解决这个问题,因此我认为在这里提出这个问题是恰当的。
如果我的代码存在许多其他问题,我很抱歉,请记住,我对编码很新。
谢谢, -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
输出的错误如下:
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__'
答案 0 :(得分:1)
我认为这里的问题是您在致电Entry self.e
之前尝试在app.mainloop()
中获取价值。
在self.actualFrame()
self.search()
,您拨打posts = r.get_subreddit(self.e.get()).get_hot(limit = 10)
拨打此电话:
self.e.get()
在尝试呼叫self.e
之前,您必须启动GUI的主循环。
我不完全清楚您的代码结构,但如果您尝试从self.search()
检索某个值,请等待app.mainloop()
直到之后 >你打电话给export