覆盖 ttk.Treeview 上的删除方法时出现问题

Problems overriding delete method on ttk.Treeview

我正在尝试创建 ttk.Treeview 的子类,主要是为了在树中保留所有已用 iid 的列表。所以我试图重写 delete 方法,但是当我尝试调用 super 的 delete 方法时总是出错。

如果我绕过删除覆盖,那么我不会收到任何错误。 但是有了它我得到了这个错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__
    return self.func(*args)
  File "/home/sean/.PyCharmCE2018.1/config/scratches/scratch_1.py", line 12, in but_handle
    sl.delete(c)
  File "/home/sean/.PyCharmCE2018.1/config/scratches/scratch_1.py", line 8, in delete
    super(Tree, self).delete(self, items)
  File "/usr/lib/python3.5/tkinter/ttk.py", line 1219, in delete
    self.tk.call(self._w, "delete", items)
_tkinter.TclError: Item .140356823468016 not found

代码如下:

import tkinter as tk
from tkinter import ttk


class Tree(ttk.Treeview):
    s = 1
    def delete(self, *items):
        super(Tree, self).delete(self, *items) # Error occurs here

        # in use i will delete the iid from a list here

def but_handle():
    for c in sl.get_children():
        sl.delete(c)

if __name__ == '__main__':
    root = tk.Tk()

    but = tk.Button(command=but_handle)
    but.pack(side='top')
    sl = Tree()
    sl.pack()

    sl.insert('', 'end', None, text='a')
    sl.insert('', 'end', None, text='b')
    sl.insert('', 'end', None, text='c')
    sl.insert('', 'end', None, text='d')
    sl.insert('', 'end', None, text='e')

    root.mainloop()

我做错了什么?

self argument 是隐式的,不应在函数调用中明确指定。

通过调用 super(Tree, self).delete(self, *items),您告诉我们删除 self 和一个 child,这当然会失败。

解决方案是将 delete 调用更改为:

super(Tree, self).delete(*items)

self. tk. call(self._w, "delete", items)

tkinter。 TclError:找不到项目 0

代码 定义视图(自我): self.tr.delete(0,结束) 因为我在 p.select():

          self.tr.insert('',END,values=i)