2011-12-06 4 views
2

wx.TreeCtrl은 (wxPython에서) 목록의 데이터를 표시하는 데 사용됩니다. 목록에서 데이터가 변경되면 트리보기가 업데이트됩니다 (즉, wx.TreeCtrl.Refresh를 호출하여 트리를 생성하는 방법)?wxPython의 wx.TreeCtrl 자동 업데이트

def Refresh(self): 
    self.CollapseAll() 
    self.Expand(self.root) 
: 나는 작품의 종류는 가상 트리를 만들고으로 새로 고침을 무시하는 것입니다 발견

data = [ 'item1', 
     ['item2', ['item2.1','item2.2'],], 
     ['item3', [['item3.1', ['item3.1.1','item3.1.2']]]],] 

하나의 솔루션 :로

목록 자체 (데이터베이스로 구성)가 구성되어

트리가 가상 일 때, 확장시 모든 노드가 목록에서 다시 읽 t집니다. 하지만 Refresh를 재정의하는 것은 아마 해킹 일 것이고 나는 더 깨끗한 해결책을 찾고있다. 그리드와 테이블 (http://svn.wxwidgets.org/viewvc/wx/wxPython/trunk/demo/Grid_MegaExample.py?view=markup)을위한 좋은 예제가 있지만, 아무것도 찾을 수 없습니다. 나무.

편집 & 답변

때로는 질문을 공식화하는 것이 가장 좋습니다 문제를 해결합니다. 나는 Rappin과 Dunn의 "wxPython in Action"에서 설명한 것처럼 가상 트리를 사용하고있었습니다. 그러나 그것은 가난한 사람의 해결책입니다. VirtualTree에서 클래스를 파생시키는 것이 맞을 것입니다. 누군가가 같은 문제에 걸려 넘어지면 여기에 해결책을 게시하십시오. 이 솔루션은 http://wxwidgets2.8.sourcearchive.com/documentation/2.8.8.0/TreeMixin_8py-source.html의 pruned-down 버전입니다.

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() 

답변

1

나는 이런 종류의 문제에 가장 적합한 솔루션은, 구체적으로는 pubsub 라이브러리를 Observer 패턴을 사용하고 있다고 생각 : wxPython and PubSub

+0

옵저버 패턴은 더 솔루션을 개선하는 데 도움이됩니다. 데이터를 변경하는 함수에서 메시지를 보내고 모든 트리가이 이벤트를 구독 할 수있게합니다 (실제 응용 프로그램에는 업데이트해야하는 여러 트리와 테이블이 있습니다). RefreshItems()를 호출 할 수 있으려면 문제의 솔루션이 여전히 필요합니다. 그러나, 당신에게 많은 감사합니다! 나는 관찰자 패턴에 대해 생각하지 않았다는 것을 당황스럽게 생각합니다. 현재 데이터를 변경 한 함수에서 필요한 모든 새로 고침 함수를 명시 적으로 호출했습니다. – bitman