0
我正在構建基於GUI的Reddit刮板應用程序的過程中,我遇到了一些問題。首先,我似乎無法讓我的第二個tkinter窗口從redditReturn類文件加載。另外,我不確定讓我的代碼段運行Reddit API以及正在運行的構建tkinter窗口的方法是否正確。唉,我主要關心的是如何糾正錯誤:tkinter基於GUI的Reddit刮板應用程序中的名稱錯誤--Python
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的元素來改變「圖片「到這行代碼中的其他東西。
再次,謝謝!
謝謝,這完美!再次,如果我的問題看起來很荒謬,我很抱歉,我對編碼很陌生。 – JeffD