Python 类型错误:__init__() 为参数“master"获得了多个值 [英] Python TypeError: __init__() got multiple values for argument 'master'

查看:81
本文介绍了Python 类型错误:__init__() 为参数“master"获得了多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在尝试用 Python 构建一个 GUI,我特别困在这部分.每次我尝试运行我的代码时,它都会抛出错误 TypeError: __init__() got multiple values for argument 'master'.我似乎无法找到在哪里传递不止一个值,这让我摸不着头脑.我尝试搜索错误,但其他人列出的修复程序我不知道如何使它们与这个一起工作.任何指导将不胜感激.请参阅下面的代码示例:

Trying to build a GUI in Python at the moment, and I'm stuck at this part in particular. Every time I try to run my code it just throws the error TypeError: __init__() got multiple values for argument 'master'. I can't seem to find where I'm passing it more than one value, and it's got me scratching my head. I tried searching the error but the fixes other people had listed I can't see how to make them work with this one. Any guidance would be much appreciated. See code sample below:

class Plotter(tk.Canvas):
    """Creates a canvas for use in a GUI
    Plotter() -> Canvas
    """
    def __init__(self, master, **kwargs):
        super().__init__(self, master = master, **kwargs)
        self.bind("<Configure>", self.on_resize)
        self.height = self.winfo_reqheight()
        self.width = self.winfo_reqwidth()
        self.bg = 'white'
        self.relief = 'raised'

class AnimalDataPlotApp(object):
    """This is the top level class for the GUI, and is hence responsible for
    creating and maintaining instances of the above glasses
    """

    def __init__(self, master):
        """Initialises the window and creates the base window for the GUI.
        __init__() -> None
        """
        master.title('Animal Data Plot App')
        self._master = master
        self._text = tk.Text(master)
        self._text.pack
        menubar = tk.Menu(master)
        master.config(menu = menubar)
        filemenu = tk.Menu(menubar) #puts filemenu into the menubar
        menubar.add_cascade(label = 'File', menu = filemenu)
        filemenu.add_command(label = 'Open', command = self.open_file)

        #frame for canvas
        plotter_frame = tk.Frame(master, bg = 'red')
        plotter_frame.pack(side = tk.RIGHT, anchor = tk.NW, fill = tk.BOTH, expand = True)

        #frame for buttons
        button_frame = tk.Frame(master, bg = 'yellow')
        button_frame.pack(side=tk.TOP, anchor=tk.NW, ipadx=50, fill = tk.X)

        #Label on the top left
        left_label = tk.Label(button_frame, text='Animal Data Sets', bg='orange')
        left_label.pack(side=tk.TOP, anchor=tk.N, fill=tk.X)

        #second frame, for selection list
        selection_frame = tk.Frame(master, bg = 'blue')
        selection_frame.pack(side = tk.LEFT, anchor=tk.NW, fill = tk.BOTH, expand = True)

        #draw buttons in frame
        select = tk.Button(button_frame, text ='Select')
        select.pack(side=tk.TOP, anchor=tk.N)
        deselect = tk.Button(button_frame, text='Deselect')
        deselect.pack(side=tk.TOP, anchor=tk.N)

        self.selectionbox = SelectionBox(selection_frame)
        self.selectionbox.pack(side = tk.TOP, expand = True, fill=tk.BOTH)
        #self._selectionbox.show_animals(self._data)

        self.plotter = Plotter(plotter_frame)
        self.plotter.pack(side = tk.TOP, expand = True, fill=tk.BOTH)

推荐答案

super().__init__(self, master = master, **kwargs)

如果您使用 super(),则无需明确指定 self.

If you're using super(), you don't need to specify self explicitly.

您收到该错误是因为 self 被解释为 master 的参数.所以这就像你在调用 __init__(master=self, master=master, **kwargs).

You are getting that error because self is being interpreted as the argument for master. So it's like if you were calling __init__(master=self, master=master, **kwargs).

这篇关于Python 类型错误:__init__() 为参数“master"获得了多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆