Search code examples
pythonpython-3.xtkintertreeviewttk

Problems overriding delete method on ttk.Treeview


I am trying to create a subclass of ttk.Treeview mostly in order to keep an list of all of the used iids in the tree. So I am trying to override the delete method but keep getting an error when I try to call the super's delete method.

If I bypass the delete override then I get no errors. But with it in I get this error:

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

Here is the code:

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()

What am I doing wrong here?


Solution

  • self argument is implicit, and should not be specified in function call explicitly.

    By calling super(Tree, self).delete(self, *items) you' telling to delete self and a child, which of course fails.

    Solution is to change delete call to:

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