TypeError:destroy()缺少1个必需的位置参数:' event'

时间:2018-04-14 09:22:49

标签: python tkinter

嗨,我一直在tkinter上面得到错误。

我一直在 tkinter 的__init__.py中搜索并销毁似乎没有得到2个参数(因此不需要事件)。

当tkinter试图销毁我创建的自定义类(这是Frame的子类)时,会调用此方法。

在我将此类添加到主窗口之前,root.destroy(),其中root是主窗口,工作正常。
在添加自定义类之后,它只销毁自定义类的一部分,然后(当可能试图销毁其他部分时)它会抛出该错误并且不会破坏根窗口。

自定义类的代码是:

class inputBox(tk.Frame):
    def __init__(self, root, parentWindow, attrName, label, valueType, query=None ):
        super().__init__(root)
        self.attrName = attrName
        self.label = tk.Label(self,text=label)
        self.valueType = valueType

        if 'enum' in valueType:
            values = valueType.replace("enum(","").replace(")","").split(",")

        elif 'varchar' in valueType:
            maxChars = int(valueType.replace("varchar(","").replace(")",""))
            if query:
                self.query=query
                success,result = DAO().executeQuery(query,'select')
                self.autoCompletedEntry = AutocompletedEntry(self, result, parentWindow, listboxLength=6, width = maxChars)
                self.label.pack()
                self.autoCompletedEntry.pack()

AutocompletedEntry(可能会产生错误的类)的代码是:

class AutocompletedEntry(tk.Frame):
    #rootsParent is needed only for the popup autocompletion list
    def __init__(self, root, autocompleteList, rootsParent, *args, **kwargs):
        super().__init__(root)

        self.rootsParent = rootsParent
        self.var = tk.StringVar(self)
        self.var.trace("w", self.changed)#lambda name, index, mode, self.var=self.var: callback(self.var))


        if 'width' in kwargs:
            self.width = int(int(kwargs['width'])//3)
            del kwargs['width']
            if self.width >40:
                self.width = 40
        else:
            self.width = 25

        self.entry = tk.Entry(self, width=self.width, textvariable=self.var)
        self.entry.pack(side='left',fill='both')

        self.button = tk.Button(self, text='▼',command=lambda: self.changed('','','arrow'))

        self.button.pack(side='right')

        # Listbox length
        if 'listboxLength' in kwargs:
            self.listboxLength = kwargs['listboxLength']
            del kwargs['listboxLength']
        else:
            self.listboxLength = 8

        # Custom matches function
        if 'matchesFunction' in kwargs:
            self.matchesFunction = kwargs['matchesFunction']
            del kwargs['matchesFunction']
        else:
            def matches(fieldValue, acListEntry):
                pattern = re.compile(re.escape(fieldValue) + '.*', re.IGNORECASE)
                return re.match(pattern, acListEntry)

            self.matchesFunction = matches

        self.focus()

        self.autocompleteList = sorted(autocompleteList)

        self.listboxUp = False        

        self.entry.bind("<Right>", self.selection)
        self.entry.bind("<Return>", self.selection)
        self.entry.bind("<Up>", self.moveUp)
        self.entry.bind("<Down>", lambda e: self.moveDown(e))
        self.entry.bind("<Escape>", self.destroy)

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

由于您使用eventdestroy,因此需要解析event作为函数的参数。因此我建议您创建function并解析event为要销毁的窗口的参数

示例:

def destroy_root(self, event):
    self.root.destroy

然后您将其用于您的活动"<Escape>",以便销毁您的window