Note that there are some explanatory texts on larger screens.

plurals
  1. POautomatic update of wxPython's wx.TreeCtrl
    text
    copied!<p>wx.TreeCtrl is used (in wxPython) to display data from a list. How to create a tree so, that the tree view is updated (i.e. by calling wx.TreeCtrl.Refresh) if data is changed in the list?</p> <p>The list itself (constructed from a database) is structured as:</p> <pre><code>data = [ 'item1', ['item2', ['item2.1','item2.2'],], ['item3', [['item3.1', ['item3.1.1','item3.1.2']]]],] </code></pre> <p>One solution I found that kind of works is to create a virtual tree and to override Refresh as:</p> <pre><code>def Refresh(self): self.CollapseAll() self.Expand(self.root) </code></pre> <p>As the tree is virtual, on expand all the nodes are read again from the list. But overriding Refresh is probably a hack and I'm looking for a cleaner solution. There are nice examples how to do it for a grid and table (http://svn.wxwidgets.org/viewvc/wx/wxPython/trunk/demo/Grid_MegaExample.py?view=markup) but I can't find anything for a tree.</p> <p><strong>EDIT &amp; ANSWER</strong></p> <p>Sometimes to solve a problem it's best to formulate the question. I was using virtual tree as described in "wxPython in Action" by Rappin and Dunn. But that's a poor man's solution. Correct would be to derive a class from VirtualTree. Posting the solution here if anyone should stumble on the same problem. The solution is a pruned-down version from (http://wxwidgets2.8.sourcearchive.com/documentation/2.8.8.0/TreeMixin_8py-source.html).</p> <pre><code>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() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload