Tkinter TreeView绑定左键单击当前树和所选项

时间:2018-04-23 06:58:11

标签: python tkinter treeview

我正在尝试将此函数self.copyTextToClipboard(self,t)绑定到多个不同的树,以使其更灵活(请参阅下面的绑定)。     来自tkinter.ttk导入Treeview     来自tkinter import *

class App:
    def __init__(self, master):
        self.master = master
        frame = Frame(master)

        master.geometry("{}x{}".format(master.winfo_screenwidth() - 100, master.winfo_screenheight() - 100))
        master.resizable(False, False)

        self.leftFrame = Frame(master, bg="#DADADA", width=375, relief=SUNKEN)
        self.leftFrame.pack_propagate(0)
        self.leftFrame.pack(side=LEFT, fill=Y, padx=1)

        # This table (TreeView) will display the partitions in the tab
        self.partitionsOpenDiskTree = Treeview(self.leftFrame, columns=("#"), show="headings", selectmode="browse", height=23)

        yscrollB = Scrollbar(self.leftFrame)
        yscrollB.pack(side=RIGHT, fill=Y)

        self.partitionsOpenDiskTree.column("#", width=50)
        self.partitionsOpenDiskTree.heading("#", text="#")
        self.partitionsOpenDiskTree.configure(yscrollcommand=yscrollB.set)

        # Bind left click on text widget to copy_text_to_clipboard() function
        self.partitionsOpenDiskTree.bind("<ButtonRelease-1>", lambda t=self.partitionsOpenDiskTree: self.copyTextToClipboard(self,t))

        # Adding the entries to the TreeView
        for i in range(3):
            self.partitionsOpenDiskTree.insert("", "end", i, values=(i), tags=str(i))

        self.partitionsOpenDiskTree.pack(anchor=NW, fill=Y)

    #todo: figure out where this is getting called and put in tree
    def copyTextToClipboard(self, tree, event=None):
        print(type(tree))
        # triggered off left button click on text_field
        root.clipboard_clear()  # clear clipboard contents
        textList = tree.item(tree.focus())["values"]
        line = ""
        for text in textList:
            if line != "":
                line += ", " + str(text)
            else:
                line += str(text)

        root.clipboard_append(line)  # append new value to clipbaord

root = Tk()
app = App(root)
root.mainloop()

但是,我似乎无法将它绑定到TreeView对象;当我运行代码时,我得到:

Exception in Tkinter callback
<class '__main__.App'>
Traceback (most recent call last):
  File "C:\Users\user1\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:/Users/user1/main_merged.py", line 56, in <lambda>
    lambda t=self.partitionsOpenDiskTree: self.copyTextToClipboard(self,t))
  File "C:/Users/user1/main_merged.py", line 70, in copyTextToClipboard
    textList = tree.item(tree.focus())["values"]
AttributeError: 'App' object has no attribute 'item'

如果我尝试打印出树型,我会发现它不是TreeView对象。关于如何获取TreeView对象的任何想法,以便我可以找出选择了哪个项目?

谢谢! -FF

2 个答案:

答案 0 :(得分:0)

当你使用bind时,回调函数必须有一个event作为它的第一个参数,后面应该放置自定义参数。但由于您的回调不需要事件参数,您可以使用lambda屏蔽它。因此,您必须更改绑定和回调的def

self.partitionsOpenDiskTree.bind("<ButtonRelease-1>", lambda event, t=self.partitionsOpenDiskTree: self.copyTextToClipboard(t))

...

def copyTextToClipboard(self, tree):

应解决问题

答案 1 :(得分:0)

所以,显然,取消自我呼叫似乎有效:

LocalDate.of(1994, 1, 1)

输出: 0