用户界面- NameError在tkinter GUI基于Reddit刮板应用程序-Python



我正在构建一个基于GUI的Reddit刮板应用程序的过程中,我遇到了一些问题。首先,我似乎无法从redditReturn类文件加载第二个tkinter窗口。此外,我不确定是否有我的代码段运行的Reddit API旁边的方法正在运行构建kinter窗口是正确的。唉,我主要关心的是如何纠正错误:

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

还有,作为旁注。如果有人知道如何改变特定的子reddit我正在刮从行:

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

我似乎无论如何也不能连接上一个条目的kinter元素来将"pics"更改为这行代码中的其他内容。

再一次,谢谢!

错误告诉您问题所在。您正在调用一个名为LabelFrame的函数或类,但是不存在这样的函数或类。所以真正的问题是,"为什么它不存在?"

因为你要像这样导入Tkinter:

import Tkinter as tk

…您需要在所有的tkinter类和命令前加上tk.。由于LabelFrame是一个tkinter小部件,您需要像这样创建它:

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

相关内容

  • 没有找到相关文章

最新更新