tkinter TypeError:函数接受1个位置参数,但给定2个

时间:2018-08-29 13:04:18

标签: python-3.x class tkinter

我正面临一个问题,我不知道这是从哪里来的。 由于我的代码太长,我将只共享产生此错误的函数和使用它的行(当然,如果需要,我当然可以共享其他部分)

class Label(object):
    #constructor
    def __init__(self, data, id,  filefullpath, AGE, counter):

        self.counter = counter 
        self.filefullpath = filefullpath
        self.data = data             
        self.object_id = id
        self.data_length = len(data)
        self.AGE = AGE
        # GUI
        self.root = Tk()
        self.root.title("CHOOSE A LABEL")
        #Create a listbox with a scrollbar
        self.listbox = Listbox(self.root)
        type = ['Indifined','a','b','c']
        for ind, val in enumerate(type):
            self.listbox.insert(ind, val)
        self.listbox.bind('<<ListboxSelect>>', self.save)
        self.listbox.pack()
        Button(self.root, text = "OK"  , command = self.root.destroy).pack(side = 'left')
        self.root.mainloop()


    def get_selected_label(self):
        """ get the label entered by the user """
        return(self.listbox.get(self.listbox.curselection())) 

    def save(self):
        """
        Save the label entered by the user
        """
        # get the label 
        label = self.get_selected_label()

        if len(label) > 0:
            for k in range(self.AGE +2): # plus 2 to include age=-1 and age = 0
                Informations = Get_Informations(self.data, self.filefullpath, self.counter - k)
                Id    = Informations.Id()
                age   = Informations.age()
                for ind, val in enumerate(Id):
                    if self.object_id == val:
                        age = age[ind]
                        if age <= self.AGE:
                            self.data[self.counter - k]["super_clusters"][ind]["label"] = label
                        else:
                            break
                    else:
                        pass
            for k in range(self.counter +1 , len(self.data)):
                Informations = Get_Informations(self.data, self.filefullpath,  k)
                Id    = Informations.Id()
                age   = Informations.age()
                if self.object_id in Id :
                    for ind, val in enumerate(Id):
                        if self.object_id == val :
                            age = age[ind]
                            if age != -1:
                                self.data[self.counter + k]["super_clusters"][ind]["label"] = label
                            else:
                                break
                        else:
                            pass
                else:
                    # the track disappeared
                    break

这是Python返回的错误:由于在调用它时我没有传递任何要保存的参数,我真的不明白该消息。

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
TypeError: save() takes 1 positional argument but 2 were given

我先谢谢你。

1 个答案:

答案 0 :(得分:2)

来自effbot(重点是我):

  

Tkinter提供了一种强大的机制,可让您自己处理事件。对于每个小部件,您都可以将Python函数和方法绑定到事件。

     

widget.bind(event, handler)

     

如果与事件描述匹配的事件发生在窗口小部件中,则给定的处理程序称为,带有描述事件的对象

因此,您在bind中定义的 handler 函数将与事件对象一起调用。该对象包含诸如生成事件的窗口小部件,事件类型等内容。在effbot链接中的事件对象下查找完整列表。

您当然可以决定对此对象不做任何事情,但是您的函数具有可以接受它,例如:

def save(self, event):