TypeError缺少位置参数?到底是怎么回事? --Python和Tkinter

时间:2015-06-02 22:42:27

标签: 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)
  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'

我完全不知道我的代码出了什么问题。我输了,网上似乎无处可回答这个问题。

这是我的代码:

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)

        StartPage.entry1 = self.entry_var.get()



'''Adding brackets around the user's entry to the label to suffice the praw api'''      
class bracketEntry(object):

    def addBrackets(self):

        user_entry = StartPage()
        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):
        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()

任何帮助表示赞赏! 谢谢!

3 个答案:

答案 0 :(得分:3)

addBrackets课程中,在user_entry = StartPage()方法中,您拨打StartPage。但是,您将__init__的{​​{1}}方法声明为def __init__(self, parent, controller):,这意味着您必须提供parentcontroller个参数。

编辑:要修复该方法,您必须将parentcontroller个对象一直传递到调用堆栈或找到另一种获取方式他们进入addBrackets方法。例如,您可以重新定义def addBrackets(self, parent, controller),然后更新侵权行:user_entry = StartPage(parent, controller)。然后,您必须更新对addBracket的所有调用以包含新参数。

答案 1 :(得分:1)

您已__init__ StartPage方法定义了parent两个必需参数,controllerStartPage()。但是在导致错误的那一行中,你只是在不传递这些参数的情况下调用bracketEntry。正如错误所说,你需要传递它们。

答案 2 :(得分:-1)

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