gpt4 book ai didi

python-3.x - tkinter 类型错误 : function takes 1 positional argument but 2 were given()

转载 作者:行者123 更新时间:2023-12-02 17:05:14 25 4
gpt4 key购买 nike

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

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

先谢谢你了。

最佳答案

来自 effbot (强调我的):

Tkinter provides a powerful mechanism to let you deal with events yourself. For each widget, you can bind Python functions and methods to events.

widget.bind(event, handler)

If an event matching the event description occurs in the widget, the given handler is called with an object describing the event.

因此,您在 bind 中定义的 handler 函数将通过事件对象调用。该对象包含生成事件的小部件、事件类型等内容。查看 effbot 链接中的 The Event Object 以获取完整列表。

你当然可以决定不对这个对象做任何事情,但是你的函数必须像这样接受它:

def save(self, event):

关于python-3.x - tkinter 类型错误 : function takes 1 positional argument but 2 were given(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52078168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com