【问题标题】:Python TypeError: __init__() got multiple values for argument 'master'Python TypeError: __init__() 为参数 'master' 获取了多个值
【发布时间】:2015-10-15 16:07:06
【问题描述】:

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

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)

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:
    super().__init__(self, master = master, **kwargs)
    

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

    您收到该错误是因为 self 被解释为 master 的参数。所以就像你打电话给__init__(master=self, master=master, **kwargs)一样。

    【讨论】:

      【解决方案2】:

      问题出现在Plotter.__init__() 中的以下行 -

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

      当调用父级的__init__ 方法时,您不需要传递self 参数,当您这样做时,它会被视为父级的master 参数,然后当您尝试传递mastermaster=master ,它会导致您的错误。

      你应该这样做 -

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-17
        • 2019-01-03
        • 1970-01-01
        • 2020-01-18
        • 2018-10-18
        • 2018-01-16
        • 2019-07-05
        • 1970-01-01
        相关资源
        最近更新 更多