使用destroy()从Tkinter中的网格中删除一个Entrybox

时间:2015-07-27 15:34:18

标签: python python-2.7 tkinter ttk

我在Tkinter中有一个树视图,双击id列中的项目(不是顶级项目),我添加了entry widget。如果我双击另一个项目,我想删除原始entry widget。我试图用entry.destroy()try语句执行此操作,这是我第一次双击时,我不会收到错误。虽然destroy()函数似乎无能为力。

from Tkinter import *
import ttk

root = Tk()

def OnDoubleClick(event):
    try:
        entry.destroy()

    except:
        entry_text = StringVar()
        entry = Entry(root, textvariable=entry_text)

        entry.configure(background='#d3d3d3')
        rowid = tree.identify_row(event.y)
        column = tree.identify_column(event.x)
        # clicked row parent id
        parent = tree.parent(rowid)

        # do nothing if item is top-level
        if parent == '':
            return
        #get column position info
        x,y,width,height = tree.bbox(rowid, column)
        print x,y,width,height


        if column == '#0':
            entry.place( x=x, y=y+height // 2, anchor=W, width=width)
            entry_text.set(tree.set(tree.identify_row(event.y)))
        print tree.set(tree.identify_row(event.y))

tree = ttk.Treeview(root)

tree["columns"]=("one","two")
tree.heading("one", text="coulmn A")
tree.heading("two", text="column B")

tree.insert("", 3, "dir3", text="Dir 3",values=("3A"," 3B"))
tree.insert("dir3", 3, 'subdir3', text="sub dir 3",values=("3A"," 3B"))
tree.insert("dir3", 3, 'subdir5', text="sub dir 4",values=("3A"," 3B"))
tree.bind("<Double-1>", OnDoubleClick)


tree.pack()
root.mainloop()

2 个答案:

答案 0 :(得分:1)

添加测试以查看条目是否存在(常见的调试测试),表明该条目不存在。提示=它与条件位置语句有关,这也意味着它是函数本地的,当函数退出时收集垃圾(从未添加到保留的根)。

from Tkinter import *
import ttk

root = Tk()

def OnDoubleClick(event):
    try:
        print "entry =", entry
    except:
        print "-----entry does not exist-----"

    try:
        entry.destroy()

    except:

        entry_text = StringVar()
        entry = Entry(root, textvariable=entry_text)

        entry.configure(background='#d3d3d3')
        rowid = tree.identify_row(event.y)
        column = tree.identify_column(event.x)
        # clicked row parent id
        parent = tree.parent(rowid)

        # do nothing if item is top-level
        if parent == '':
            return
        #get column position info
        x,y,width,height = tree.bbox(rowid, column)
        print x,y,width,height


        if column == '#0':
            entry.place( x=x, y=y+height // 2, anchor=W, width=width)
            entry_text.set(tree.set(tree.identify_row(event.y)))
        print tree.set(tree.identify_row(event.y))

tree = ttk.Treeview(root)

tree["columns"]=("one","two")
tree.heading("one", text="coulmn A")
tree.heading("two", text="column B")

tree.insert("", 3, "dir3", text="Dir 3",values=("3A"," 3B"))
tree.insert("dir3", 3, 'subdir3', text="sub dir 3",values=("3A"," 3B"))
tree.insert("dir3", 3, 'subdir5', text="sub dir 4",values=("3A"," 3B"))
tree.bind("<Double-1>", OnDoubleClick)


tree.pack()
root.mainloop()

答案 1 :(得分:1)

Python认为entry是一个局部变量。因为您尝试过,所以您没有看到它给出的错误,这类似于UnboundLocalError: local variable 'entry' referenced before assignment