自动更新wxPython';s wx.TreeCtrl

自动更新wxPython';s wx.TreeCtrl,wxpython,Wxpython,使用wx.TreeCtrl(在wxPython中)显示列表中的数据。如何创建树,以便在列表中的数据发生更改时更新树视图(即通过调用wx.TreeCtrl.Refresh) 列表本身(由数据库构建)的结构如下: data = [ 'item1', ['item2', ['item2.1','item2.2'],], ['item3', [['item3.1', ['item3.1.1','item3.1.2']]]],] 我发现的一种解决方案是创建一个虚拟树

使用wx.TreeCtrl(在wxPython中)显示列表中的数据。如何创建树,以便在列表中的数据发生更改时更新树视图(即通过调用wx.TreeCtrl.Refresh)

列表本身(由数据库构建)的结构如下:

data =  [ 'item1',
         ['item2', ['item2.1','item2.2'],],
         ['item3', [['item3.1', ['item3.1.1','item3.1.2']]]],]
我发现的一种解决方案是创建一个虚拟树,并将刷新覆盖为:

def Refresh(self):
    self.CollapseAll()
    self.Expand(self.root)
由于树是虚拟的,在展开时,将再次从列表中读取所有节点。但覆盖刷新可能是一个黑客,我正在寻找一个更干净的解决方案。这里有一些很好的例子来说明如何对网格和表格执行此操作(http://svn.wxwidgets.org/viewvc/wx/wxPython/trunk/demo/Grid_MegaExample.py?view=markup)但是我找不到一棵树的任何东西

编辑和回答

有时候,为了解决一个问题,最好是制定一个问题。我使用了虚拟树,如Rappin和Dunn在《wxPython在行动》中所描述的。但这是穷人的解决办法。正确的做法是从VirtualTree派生类。如果有人遇到同样的问题,请在此处发布解决方案。解决方案是从(http://wxwidgets2.8.sourcearchive.com/documentation/2.8.8.0/TreeMixin_8py-source.html)


我认为解决这类问题最合适的方法是使用模式,特别是pubsub library:

Observer模式有助于进一步改进解决方案。它可以从更改数据的函数发送消息,所有树都可以订阅此事件(在实际应用程序中,有几个树和表需要更新)。仍然需要问题的解决方案,以便能够调用RefreshItems()。不过,非常感谢您的回答!我很尴尬,我没有考虑观察者模式。目前,我在更改数据的函数中显式调用了所有需要的刷新函数。
import wx
from wx.lib.mixins.treemixin import VirtualTree
items = [('item 0', [('item 2', [('a1', []),('b1', [])]), ('item 3', [])]), 
         ('item 1', [('item 4', [('a3', []),('b3', [])]), ('item 5', [])])]

class MyTree(VirtualTree, wx.TreeCtrl):
    def __init__(self, *args, **kw):
        super(MyTree, self).__init__(*args, **kw)
        self.RefreshItems()
        #OnTest emulates event that causes data to change
        self.Bind(wx.EVT_KEY_DOWN, self.OnTest) 
    def OnTest(self, evt): 
        items[0]=('boo', [('item 2', [('a1', []),('b1', [])]), ('item 3', [])])
        self.RefreshItems()        
    def OnGetItemText(self, index):
        return self.GetText(index)
    def OnGetChildrenCount(self, indices):
        return self.GetChildrenCount(indices)
    def GetItem(self, indices):
        text, children = 'Hidden root', items
        for index in indices: text, children = children[index]
        return text, children
    def GetText(self, indices):
        return self.GetItem(indices)[0] 
    def GetChildrenCount(self, indices):
        return len(self.GetChildren(indices))
    def GetChildren(self, indices):
        return self.GetItem(indices)[1]    

class TreeFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='wxTree Test Program')
        self.tree = MyTree(self, style=wx.TR_DEFAULT_STYLE | wx.TR_HIDE_ROOT)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = TreeFrame()
    frame.Show()
    app.MainLoop()