데모 코드는 http://wiki.wxpython.org/AnotherTutorial#wx.TreeCtrl에서 시작되었습니다. build_tree, _build_tree_helper 및 build_conn_dict 메소드를 추가했습니다. dot_parser 라이브러리에서 중요한 관심 대상은 "연결"사전을 만드는 데 사용되는 edge.get_source() 및 edge.get_destination()입니다.
도트 그래프는 dot_data 변수에 저장됩니다. 중요하게 도트 그래프 은 루프가 아니어야합니다; 즉, 스패닝 트리 여야합니다. 그렇지 않으면 _build_tree_helper 메서드가 무한 루프됩니다 (TreeControl에서는 이해가되지 않습니다).
또한 작동시키기 위해서 dot_parser를 https://github.com/nlhepler/pydot-py3/issues/1#issuecomment-15999052에 따라 패치해야했습니다.
import wx
from dot_parser import parse_dot_data
class MyFrame(wx.Frame):
def __init__(self, parent, id, title, **kwargs):
self.parsed_dot = kwargs.pop("parsed_dot", None)
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(450, 350))
hbox = wx.BoxSizer(wx.HORIZONTAL)
vbox = wx.BoxSizer(wx.VERTICAL)
panel1 = wx.Panel(self, -1)
panel2 = wx.Panel(self, -1)
self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1,-1), wx.TR_HAS_BUTTONS | wx.TR_LINES_AT_ROOT)
self.build_tree(self.tree)
self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)
self.display = wx.StaticText(panel2, -1, "",(10,10), style=wx.ALIGN_CENTRE)
vbox.Add(self.tree, 1, wx.EXPAND)
hbox.Add(panel1, 1, wx.EXPAND)
hbox.Add(panel2, 1, wx.EXPAND)
panel1.SetSizer(vbox)
self.SetSizer(hbox)
self.Centre()
def build_conn_dict(self):
conn_dict = {}
if(self.parsed_dot):
for edge in self.parsed_dot.get_edges():
conn_dict.setdefault(edge.get_source(), []).append(edge.get_destination())
return conn_dict
def build_tree(self, tree):
if(self.parsed_dot):
conn_dict = self.build_conn_dict()
outs = set(conn_dict.keys())
ins = reduce(lambda x, y: x | set(y), conn_dict.values(), set([]))
roots = list(outs - ins)
roots = dict([(root, tree.AddRoot(root)) for root in roots])
self._build_tree_helper(tree, conn_dict, roots)
def _build_tree_helper(self, tree, conn_dict = {}, roots = {}):
new_roots = {}
for root in roots:
if(conn_dict.has_key(root)):
for sub_root in conn_dict[root]:
new_roots[sub_root] = tree.AppendItem(roots[root], sub_root)
if(new_roots):
self._build_tree_helper(tree, conn_dict, new_roots)
def OnSelChanged(self, event):
item = event.GetItem()
self.display.SetLabel(self.tree.GetItemText(item))
child_text = self.tree.GetItemText(item)
parent_text = ""
try:
parent = self.tree.GetItemParent(item)
parent_text = self.tree.GetItemText(parent)
except wx._core.PyAssertionError:
pass
print "child: %s, parent: %s" % (child_text, parent_text)
class MyApp(wx.App):
def OnInit(self):
dot_data = \
'''
graph ""
{
label="(% (EXP (% (X) (% (X) (X)))) (EXP (SIN (X))))"
n039 ;
n039 [label="%"] ;
n039 -> n040 ;
n040 [label="EXP"] ;
n040 -> n041 ;
n041 [label="%"] ;
n041 -> n042 ;
n042 [label="X"] ;
n041 -> n043 ;
n043 [label="%"] ;
n043 -> n044 ;
n044 [label="X"] ;
n043 -> n045 ;
n045 [label="X"] ;
n039 -> n046 ;
n046 [label="EXP"] ;
n046 -> n047 ;
n047 [label="SIN"] ;
n047 -> n048 ;
n048 [label="X"] ;
}
'''
parsed_dot = parse_dot_data(dot_data)
frame = MyFrame(None, -1, "treectrl.py", parsed_dot = parsed_dot)
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()
이것은 재미 있지만, 내가 찾고있는 것이 아닙니다. 물리적 인 트리와 객체 (예 : 노드를 클릭하고 주변을 이동하는 등)와 상호 작용하기를 원합니다. 단지 treectrl로 시각화하는 것이 아니라 –
https://gephi.org/를 원합니다. 필자는 다른 사람들과 함께 도트 파일을 읽을 수 있다고 생각합니다. 자바로 작성되었지만 자이 썬 (Jython) 인터프리터를 필요에 따라 파이썬을 사용하여 제어 할 수있다. – dilbert