tkinter TypeError:函数需要 1 个位置参数,但给出了 2 个()

B.S*_*rah 2 class tkinter python-3.x

我遇到了一个问题,我不知道这个问题从何而来。由于我的代码太长,我只会共享生成此错误的函数以及使用它的行(如果有必要,我当然可以共享其他部分)

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
Run Code Online (Sandbox Code Playgroud)

这是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
Run Code Online (Sandbox Code Playgroud)

我提前谢谢你。

fhd*_*sdg 6

来自effbot(强调我的):

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

widget.bind(event, handler)

如果小部件中发生与事件描述匹配的事件,则使用描述该事件的对象调用给定的处理程序。

因此,您在 中定义的处理bind函数将通过事件对象进行调用。该对象包含生成事件的小部件、事件类型等内容。查看effbot 链接中的“事件对象”下的完整列表。

您当然可以决定不对这个对象执行任何操作,但是您的函数必须接受它,如下所示:

def save(self, event):
Run Code Online (Sandbox Code Playgroud)