简体   繁体   English

在 ttk.Treeview 上覆盖删除方法的问题

[英]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.我试图创建 ttk.Treeview 的子类,主要是为了保留树中所有使用的 iid 的列表。 So I am trying to override the delete method but keep getting an error when I try to call the super's delete method.所以我试图覆盖 delete 方法,但是当我尝试调用 super 的 delete 方法时不断收到错误。

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?我在这里做错了什么?

self argument is implicit, and should not be specified in function call explicitly. self argument是隐式的,不应在函数调用中显式指定。

By calling super(Tree, self).delete(self, *items) you' telling to delete self and a child, which of course fails.通过调用super(Tree, self).delete(self, *items)你告诉删除self和一个孩子,这当然失败了。

Solution is to change delete call to:解决方案是将delete调用更改为:

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

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

tkinter .特金特。 TclError: Item 0 not found TclError:找不到项目 0

code def View(self): self.tr.delete(0,END) for i in p.select():代码 def View(self): self.tr.delete(0,END) for i in p.select():

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM