首页 > 解决方案 > 当我运行代码时,PRAW 连接的 Tkinter GUI 显示为空白

问题描述

我正在编写一个连接到 reddits API 的程序。我为用户设置了三个条目小部件,以输入特定的单词、特定的 subreddit 和要解析的特定帖子数。

下面贴出代码。我将所有这些都放在一个名为 KDGUI 的类中。当我运行脚本时,我只是得到一个空白的白色 Tkinter GUI 屏幕。

有任何想法吗?我试过在课堂上来回切换 GUI 代码。我还没有足够的经验来真正弄清楚这一点。

谢谢!

这是在崇高的文本上。我没有发布 API 信息,但想象它在根变量之上。

root = tk.Tk()

Ht = 300
Wd = 450


class KDGUI():

    def enter_info():
        word = e1.get()
        subred = e2.get()
        amnt = e3.get()

        def run_bot():
            sub = r.subreddit(subred)
            time.sleep(1)
            print("---Grabbing subreddit---\n")
            time.sleep(1)
            subs = sub.top('day', limit=amnt)
            print("---Grabbing posts in sub---\n")
            time.sleep(1)
            print("Looking for Articles..\n")

            word_not_found = True

            for posts in subs:
                article_url = posts.url
                post_title = posts.title
                word_to_find = word

                word_not_found = False

                if word_to_find in post_title:
                    time.sleep(3)
                    print(post_title)
                    print('')



                else: 
                    word_not_found = word_not_found and True

                if word_not_found:
                    print("Can't find articles in the subreddit!")




        root.title("Finding Kawhi Articles")

        canvas = tk.Canvas(root, height=Ht, width=Wd)
        canvas.pack()

        background_image = tk.PhotoImage(file='Kawhi-Leonard.gif')

        background_label = tk.Label(root, image=background_image)
        background_label.place(x=0, y=0, relwidth=1, relheight=1)

        e1 = tk.Entry(root) # Word to enter
        e1.place(relx=.275, rely=.45)

        e2 = tk.Entry(root) # subreddit to enter
        e2.place(relx=.275, rely=.55)

        e3 = tk.Entry(root) # sort by amount of posts
        e3.place(relx=.275, rely=.65)

        activate_button = tk.Button(root, text="Click here to generate 
article!", command=run_bot)
        activate_button.place(relx=.20, rely=.85, relwidth=0.55, 
relheight=0.10)

        close_button = tk.Button(root, text="Click to close program", 
command = root.quit)
        close_button.place()

        root.resizable(False, False)





root.mainloop()
KDGUI()

标签: pythontkinterredditpraw

解决方案


推荐阅读