入口部件快把我逼疯了!: Tkinter Reddit Scraper认为字符串条目是数字



我在一个小项目的中间,创建一个tkinter gui,从reddit.com使用他们的api从用户定义的subreddit输出前十个帖子。因为我需要有subreddit是用户的选择,它需要使用tkinter条目小部件输入。然而,我遇到的问题是,当用户输入信息时,小部件不会刷新,相反,它只是在程序启动时输出一串数字(".4302552760.4302552816.4367528344")。我似乎不能让这个东西仅仅记录用户输入的字符串变量所以我可以在接下来的几个类中运行它让这该死的东西工作。我在这里遇到了真正的障碍,非常感谢你的帮助。

我先给出几个代码示例,然后是整个代码,然后是具体的问题区域:

整个代码如下:

import tkinter as tk
from functools import partial
from webbrowser import open
from datetime import date
import praw

'''Initialising the Applicaiton'''
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()
'''The First Page the User will see'''
class StartPage(tk.Frame, object):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label1 = tk.Label(self, text="Start Page")
        label1.pack(pady=10, padx=10)
        '''label2 = tk.Label(self, text="Confirm This Subreddit", command= confirmSubreddit())
        label2.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)
        StartPage.entry1 = e1

'''Adding brackets around the user's entry to the label to suffice the praw api'''      
class bracketEntry(object):
    def addBrackets(self):
        user_entry_plus_brackets = '"' + str(StartPage.entry1) + '"'
        print(user_entry_plus_brackets)
        return user_entry_plus_brackets

'''Collecting data from reddit'''
class redditCollect(object):
    def getSubreddit(self):
        user_agent = "Simple Subreddit Scraper"
        r = praw.Reddit(user_agent=user_agent)
        '''remember to add the ability to get the user-defined subreddit information'''
        user_entry = bracketEntry()
        user_entry_variable = user_entry.addBrackets()
        posts = r.get_subreddit("pics").get_hot(limit = 10)
        return posts

'''The window containing the information from Reddit for the user'''        
class redditReturn(tk.Frame, object):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        """Creates all the buttons and frames for the GUI"""
        get_user_entry = bracketEntry()
        get_user_entry_string = get_user_entry.addBrackets()
        intro = get_user_entry_string + " on Reddit: "
        newFrame = tk.LabelFrame(self, text = intro)
        newFrame.pack(fill="both", expand= True , anchor="nw")        
        row = 0
        redditCollectGetter = redditCollect()
        local_posts = redditCollectGetter.getSubreddit()
        for p in local_posts:
            gotoArticle = partial(open, p.url)
            title = "(" + str(p.score) +") " + p.title
            tk.Label(newFrame, text= title, pady= 10, wraplength= 700, justify= "left").grid(row= row, column= 0, sticky= "w")
            tk.Button(newFrame, text= "Read more!", command= gotoArticle).grid(row= row+1, column= 0, sticky= "w")
            tk.row = row + 2


app = RedditScraper()
app.mainloop()

下面是我定义条目小部件的类(如果这对您有帮助的话):

class StartPage(tk.Frame, object):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label1 = tk.Label(self, text="Start Page")
        label1.pack(pady=10, padx=10)
        '''label2 = tk.Label(self, text="Confirm This Subreddit", command= confirmSubreddit())
        label2.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)
        StartPage.entry1 = e1

这里是我第一次尝试操纵条目,在它周围添加括号,这样它就可以与reddit api一起工作:

class bracketEntry(object):
    def addBrackets(self):
        user_entry_plus_brackets = '"' + str(StartPage.entry1) + '"'
        print(user_entry_plus_brackets)
        return user_entry_plus_brackets

我把print(user_entry_plus_brackets)放在这里是为了显示它输出的是一串数字,而不是用户输入的字符串。

我对python(和一般的编码)非常陌生,可能已经超出了我的头脑…任何帮助都是非常感激的!

谢谢!

关于小部件的事情是,当您将它们强制转换为字符串时,它们将返回其tkinter id。您需要做的是创建一个控制变量(http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/control-variables.html),并将其与您的条目关联起来。

self.entry_var = tk.StringVar()
e1 = tk.Entry(self,textvariable=self.entry_var)

而不是像你做的那样从条目中检索值:

str(StartPage.entry1)

你会得到这样的控制变量的值:

user_entry_plus_brackets = '"' + self.entry_var.get() + '"'

我现在不在家,在那里我可以测试出来,但我相信你应该能够得到Entry的值使用它的。get()方法,而不需要使用控制变量:

user_entry_plus_brackets = '"' + self.entry1.get() + '"'

相关内容

  • 没有找到相关文章

最新更新