私はこのツリー状の構造のためのクラスを書いた:
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)
何の問題あなたはこのアイデアを解決しようとしていますか? 「変化を引き起こす」と言えば、変化の性質は何ですか? Bが "Hello"に設定されている場合、DとEをどのように設定すると思いますか? –