我正在构建基于GUI的Reddit scraper应用程序,我遇到了一些问题。首先,我似乎无法从redditReturn类文件加载我的第二个tkinter窗口。另外,我不确定运行Reddit API的代码部分以及运行构建tkinter窗口的方法是否正确。唉,我主要担心的是如何纠正错误:
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元素以改变&#34; pics&#34;在这行代码中的其他内容。
再次,谢谢!
答案 0 :(得分:1)
错误正在告诉您问题的确切原因。您正在调用名为#ff123456
的函数或类,但不存在此类函数或类。所以真正的问题是,&#34;为什么它不存在?&#34;
因为您正在导入Tkinter:
LabelFrame
...您需要使用import Tkinter as tk
为所有tkinter类和命令添加前缀。由于tk.
是tkinter小部件,因此您需要像这样创建它:
LabelFrame