Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 在Tkinter树中选择多个节点时,打印每个选择的父名称_Python 2.7_Tkinter_Tree_Treeview_Tkinter Layout - Fatal编程技术网

Python 2.7 在Tkinter树中选择多个节点时,打印每个选择的父名称

Python 2.7 在Tkinter树中选择多个节点时,打印每个选择的父名称,python-2.7,tkinter,tree,treeview,tkinter-layout,Python 2.7,Tkinter,Tree,Treeview,Tkinter Layout,我希望用户从Tkinter树的不同分支中选择多个节点。为了进行进一步的处理,我应该知道每个选择的父分支 当我只选择一个节点时,我可以使用下面的代码获得父id。 当我按ctrl键选择多个节点时,我只得到第一个选择的父节点 如何同时完成所有选择的父节点 这是我的工作代码: import ttk import Tkinter as tk def select(): item_iid = tree.selection()[0] parent_iid = tree.parent(item

我希望用户从Tkinter树的不同分支中选择多个节点。为了进行进一步的处理,我应该知道每个选择的父分支

当我只选择一个节点时,我可以使用下面的代码获得父id。 当我按ctrl键选择多个节点时,我只得到第一个选择的父节点 如何同时完成所有选择的父节点

这是我的工作代码:

import ttk
import Tkinter as tk

def select():
    item_iid = tree.selection()[0]
    parent_iid = tree.parent(item_iid)
    node = tree.item(parent_iid)['text']
    print node

root = tk.Tk()
tree = ttk.Treeview(root,show="tree")#, selectmode=EXTENDED)  
tree.config(columns=("col1"))

#SUb treeview
style = ttk.Style(root)
style.configure("Treeview")
tree.configure(style="Treeview")

tree.insert("", "0", "item1", text="Branch1",)
tree.insert("", "1", "item2", text="Branch2")

#sub tree using item attribute to achieve that
tree.insert("item1", "1", text="FRED")
tree.insert("item1", "1", text="MAVIS")
tree.insert("item1", "1", text="BRIGHT")

tree.insert("item2", "2", text="SOME")
tree.insert("item2", "2", text="NODES")
tree.insert("item2", "2", text="HERE")

tree.pack(fill=tk.BOTH, expand=True)
tree.bind("<Return>", lambda e: select()) 

root.mainloop()
电流输出:

仅选择一个节点时能够显示父节点名称

完成后,将仅显示第一个节点的多个选择父节点,所选每个节点应具有父节点名称

Branch1仅显示在第一个选择中:

挑选

返回选定项的元组

重点矿山

.selection返回树视图中选定的所有项的元组。在函数的第一行,您仅显式选择第一项:

def select():
    item_iid = tree.selection()[0] #<---Right here you tell Python that you only want to use the first item from the tuple.
    parent_iid = tree.parent(item_iid)
    node = tree.item(parent_iid)['text']
    print node

谢谢@Ethan我还需要一个帮助我可以不按ctrl键从树中选择多个选项吗。我可以在列表框中以浏览模式多次执行此操作,但在树视图中无法执行相同的操作这是一个单独的问题。如果你需要一个新的答案,请提出一个新问题。我已经提出了一个问题,先生,我请你看一次,给出了与参考链接相同的代码:你的输入将非常有用。你链接到了同一个问题?链接:@Ethan请在这里找到更新的链接
def select():
    for i in tree.selection():
        item_iid = i
        parent_iid = tree.parent(item_iid)
        node = tree.item(parent_iid)['text']
        print(node)