Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
如何向wx.TreeCtrl项添加额外数据?wxpython_Python_User Interface_Wxpython - Fatal编程技术网

如何向wx.TreeCtrl项添加额外数据?wxpython

如何向wx.TreeCtrl项添加额外数据?wxpython,python,user-interface,wxpython,Python,User Interface,Wxpython,如何向下面名为fileTree的wx.TreeCtrl中附加的树项添加额外数据。我将文本文件中的数据读入数组以构建树。树中的append项是图像的文件名,但我想知道如何为每个项添加完整的文件路径。当我双击一个项目时,必须显示完整的文件路径,而不仅仅是它的文件名 def __init__(self, *args, **kwds): self.fileTree = wx.TreeCtrl(self, size=(200, 100)) self.root = self.fileTre

如何向下面名为
fileTree
wx.TreeCtrl
中附加的树项添加额外数据。我将文本文件中的数据读入数组以构建树。树中的append项是图像的文件名,但我想知道如何为每个项添加完整的文件路径。当我双击一个项目时,必须显示完整的文件路径,而不仅仅是它的文件名

def __init__(self, *args, **kwds):

    self.fileTree = wx.TreeCtrl(self, size=(200, 100))
    self.root = self.fileTree.AddRoot('Images')
    self.allImages = self.fileTree.AppendItem(self.root, 'All')
    imgLst=self.generateList(imagelist)# Reads a text file with filepaths into list

    for item in imgLst:
        tmp=item
        tmp = tmp[tmp.rfind("\\")+1:tmp.rfind(".")]
        self.fileTree.AppendItem(self.allImages, tmp)

    self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.onTreeDClick, self.fileTree)

def onTreeDClick(self,event):
    print 'Double clicked on', self.fileTree.GetItemText(event.GetItem())

def generateList(self, fname):
    f = open(fname, "rb")

    a=[]
    for line in f:
        a.append(line.strip())
    return a
在其中,您可以有一些与之关联的数据项。此数据是一个
wx.TreeItemData
类实例,可以包含任意Python对象。让我们用一个代码来澄清这一点:

# Let fullPath be holding full path you your file (i.e. in Python string)
fileInfo = wx.TreeItemData(fullPath)
fileInfo
对象实例可与任意
wx.TreeCtrl
项关联:

# Let item variable contain your tree item object
self.fileTree.SetPyData(item, fileInfo)
这里,在上面的代码中,我们将item对象与
fileInfo
对象相关联。您可以访问事件处理程序中的关联对象:

self.fileTree.GetPyData(event.GetItem())

就这些。注意,任何Python对象都可以与
wx.TreeCtrl
项相关联

@Rostyslav Dzinko感谢您的编辑,有什么想法吗?谢谢,在使用self.fileTree.GetPyData(event.GetItem())时,如何获取对象的文本?这很有效->>currentImg=self.fileTree.GetPyData(event.GetItem())打印currentImg.GetData()