【发布时间】:2015-06-03 06:09:40
【问题描述】:
我在做一个小项目,创建一个 tkinter gui,使用他们的 api 从 reddit.com 的用户定义的 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 非常陌生(和一般的编码),也许已经超出了我的头脑......非常感谢任何帮助!
谢谢!
【问题讨论】:
标签: python user-interface tkinter reddit scraper