2016-12-22 16 views
-1

StringVarのツリー構造を構築したいと思います。次の図は、私が達成したいことを(うまくいけば)説明しています。TreeVar構造体をツリー順序で構築する方法

      A 
         / \ 
        / \ 
         B  C 
        /\ 
        / \ 
        D  E 

Bでの変更は、他のSTRINGVARの彼下記(DおよびE)に変更をトリガする必要があり、しかし、それはA.

に変更をトリガべきではない、これは行うことができますか?

+1

何の問題あなたはこのアイデアを解決しようとしていますか? 「変化を引き起こす」と言えば、変化の性質は何ですか? Bが "Hello"に設定されている場合、DとEをどのように設定すると思いますか? –

答えて

0

私はこのツリー状の構造のためのクラスを書いた:

from tkinter import Tk, StringVar 

class StringVarTree: 
    """ Tree of StringVars """ 
    def __init__(self, node, left=None, right=None): 
     """ 
      node: StringVar 
      left: StringVarTree or None 
      right: StringVarTree or None 
     """ 
     self.node = node 
     self.left = left 
     self.right = right 

    def __repr__(self): 
     return "%s [%s %s]" % (self.node.get(), self.left, self.right) 

    def set(self, string): 
     # modify the node 
     self.node.set(string) 
     # propagate the modification to all branches below the node 
     if self.left: 
      self.left.set(string) 
     if self.right: 
      self.right.set(string) 


if __name__ == '__main__': 
    root = Tk() 
    A = StringVar(root, "A") 
    B = StringVar(root, "B") 
    C = StringVar(root, "C") 
    D = StringVar(root, "D") 
    E = StringVar(root, "E") 
    # tree of StringVars 
    tree = StringVarTree(A, StringVarTree(B, StringVarTree(D), StringVarTree(E)), StringVarTree(C)) 
    # thanks to the __repr__ method, we can see the tree 
    print(tree) 
    # now let's modify the B branch 
    tree.left.set("b") 
    print(tree) 
関連する問題