python - wx.Treetrl 项目查找表

标签 python wxpython

我正在尝试创建一个查找表以将 wxTreeItem 连接到对象。选择或双击该项目后,应对此对象采取行动。 奇怪的是,我发现 AppendItem 之后返回的项目实例是附加到树的真实项目的副本,或者是 self.tree.GetSelection() 事件。 GetItem() 返回相关项目的副本。

import wx

class RandomObj(object):
    def __init__(self, name):
        self.name = name

class TreeExample(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='Tree Example', size=(200, 130))
        self.tree = wx.TreeCtrl(self, size=(200, 100))

        root = self.tree.AddRoot('root')
        self.itemLUT = {}
        for obj in [RandomObj('item1'), RandomObj('item2'), RandomObj('item3')]:
            item = self.tree.AppendItem(root, obj.name)
            print item
            self.itemLUT[id(item)] = obj
            self.itemLUT[id(obj)] = item

        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated, self.tree)
        self.tree.Expand(root)

    def OnActivated(self, event):
        item = event.GetItem()
        print 'Double clicked on', self.tree.GetItemText(item)
        print id(item) in self.itemLUT.keys()
        print self.tree.GetSelection()
        print item

app = wx.PySimpleApp(None)
TreeExample().Show()
app.MainLoop()

有什么建议吗?是否有任何正确的方法可以根据树项目上的操作(鼠标或键盘)连接和访问对象。

最佳答案

最好的方法就是使用 SetItemData 将数据放入项目中:

item = self.tree.AppendItem(root, obj.name)
self.tree.SetItemData(item,obj)

稍后,您可以使用 GetItemData 从项目中提取数据。您几乎可以在其中放入任何东西。

关于python - wx.Treetrl 项目查找表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22379248/

相关文章:

python - 使用 SQLAlchemy 和 wxPython 的桌面应用程序的项目结构

python - Pandas 数据帧 : Check if regex contained in a column matches a string in another column in the same row

python - App Engine 数据存储区通过引用过滤器连接

python - 在 wxPython 中刷新面板内容

python - wxPython - 创建一个在我的 TextCtrl 面板中生成位图图像的按钮

python - 如何使 wx 工具栏按钮变大?

python - 如何在Python中使用mysqldb从mysql db获取数据并将它们插入到wx.ComboBox中

python - 如何处理python 3中的错误 "too many arguments"

python - 在 pandas 交叉表中合并计数和百分比(标准化)

python - 从 S3 存储桶导入 AWS Lambda 函数代码中的库