TypeError是否缺少位置参数?发生了什么事--Python和Tkinter
我试图编写一个简单的tkinter程序,将reddit信息返回给用户。这样做时,我收到了错误消息:TypeError是否缺少位置参数?发生了什么事--Python和Tkinter,python,user-interface,tkinter,Python,User Interface,Tkinter,我试图编写一个简单的tkinter程序,将reddit信息返回给用户。这样做时,我收到了错误消息: Traceback (most recent call last): File "redditscraper4.py", line 111, in <module> app = RedditScraper() File "redditscraper4.py", line 23, in __init__ frame = F(container, self) F
Traceback (most recent call last):
File "redditscraper4.py", line 111, in <module>
app = RedditScraper()
File "redditscraper4.py", line 23, in __init__
frame = F(container, self)
File "redditscraper4.py", line 93, in __init__
get_user_entry_string = get_user_entry.addBrackets()
File "redditscraper4.py", line 62, in addBrackets
user_entry = StartPage()
TypeError: __init__() missing 2 required positional arguments: 'parent' and 'controller'
感谢您的帮助!
谢谢 您已经定义了
StartPage
的\uuuuu init\uuuu
方法来获取两个必需的参数,parent
和controller
。但是在导致错误的行中,您只是调用StartPage()
,而没有传递这些参数。正如错误所说,您需要传递它们。在bracketEntry
类中,在addBrackets
方法中,调用user\u entry=StartPage()。但是,您将StartPage
的\uuuuu init\uuuuu
方法声明为def\uuuu init\uuuuuu(self、parent、controller):
,这意味着您必须提供parent
和controller
参数
编辑:要修复该方法,您必须将父对象
和控制器
对象一直传递到调用堆栈中,或者找到另一种方法将它们放入添加括号
方法中。例如,您可以重新定义def add括号(self、parent、controller)
,然后更新侵权行:user\u entry=StartPage(parent、controller)
。然后,您必须更新对addBracket
的所有调用以包含新参数。我将bracketEntry类中的行更改为def addbrates(parent,controller):但添加另一个“controller”位置参数时仍会出错。谢谢你的帮助@JeffD我已经添加了一些关于如何解决问题的详细信息。要问自己的关键问题是“我在哪里定义父窗口小部件和控制器?”以及“我如何将这些对象传递到实际调用?”请在代码中添加一些解释,以便其他人可以从中学习
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)
button1 = tk.Button(self, text="Scrape This Subreddit", command=lambda: controller.show_frame(redditReturn))
button1.pack(pady=10, padx=10)
self.entry_var = tk.StringVar()
e1 = tk.Entry(self, textvariable=self.entry_var)
e1.pack(pady=10, padx=10)
global save
save = tk.StringVar()
save = e1.get()
# StartPage.entry1: str = self.entry_var.get()
def entry1(self):
global save
user_input = tk.StringVar()
user_input = save
return save
'''Adding brackets around the user's entry to the label to suffice the praw api'''
class bracketEntry(object):
def addBrackets(self, parent, controller):
user_entry = StartPage(parent, controller)
# * Takes in inputs as parent and the controller
get_user_entry_string = user_entry.entry1()
user_entry_plus_brackets = '"' + get_user_entry_string + '"'
print(user_entry_plus_brackets)
return user_entry_plus_brackets
'''Collecting data from reddit'''
class redditCollect(object):
def getSubreddit(self, parent, controller):
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(parent,controller)
print(user_entry_variable) # prints the quoted string in the Entry field
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()
# * takes in no inputs
get_user_entry_string = get_user_entry.addBrackets(parent, controller)
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(parent,controller)
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()
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)
button1 = tk.Button(self, text="Scrape This Subreddit", command=lambda: controller.show_frame(redditReturn))
button1.pack(pady=10, padx=10)
self.entry_var = tk.StringVar()
e1 = tk.Entry(self, textvariable=self.entry_var)
e1.pack(pady=10, padx=10)
global save
save = tk.StringVar()
save = e1.get()
# StartPage.entry1: str = self.entry_var.get()
def entry1(self):
global save
user_input = tk.StringVar()
user_input = save
return save
'''Adding brackets around the user's entry to the label to suffice the praw api'''
class bracketEntry(object):
def addBrackets(self, parent, controller):
user_entry = StartPage(parent, controller)
# * Takes in inputs as parent and the controller
get_user_entry_string = user_entry.entry1()
user_entry_plus_brackets = '"' + get_user_entry_string + '"'
print(user_entry_plus_brackets)
return user_entry_plus_brackets
'''Collecting data from reddit'''
class redditCollect(object):
def getSubreddit(self, parent, controller):
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(parent,controller)
print(user_entry_variable) # prints the quoted string in the Entry field
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()
# * takes in no inputs
get_user_entry_string = get_user_entry.addBrackets(parent, controller)
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(parent,controller)
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()