2017-08-28 117 views
0

点击更新按钮后,alist会更新,我如何获取更新值并可以在组合框中选择?非常感谢!如何跟踪变量?

namespace eval PreGen { 
    set alist {sec1 sec2 sec3 sec4} 
    proc SetUp {} { 
     ttk::combobox .c -values $PreGen::alist 
     button .b -text update -command PreGen::Update 
     grid ... 
    } 
    proc Update {} { 
     ... 
     set PreGen::alist {op1 op2 op3 ...} #the list value got from other file 
     ... 
    } 
} 

回答

0

您可以很容易地添加跟踪。使用帮助程序实现跟踪回调最为明显,但您可以使用apply这个术语;那也会起作用,但是代码更不透明,因为它在一行上更多。这里的程序版本:

namespace eval PreGen { 
    # ALWAYS use [variable] to set default values for variables 
    variable alist {sec1 sec2 sec3 sec4} 

    proc SetUp {} { 
     variable alist 
     ttk::combobox .c -values $alist 
     trace add variable alist write ::PreGen::AlistUpdated 
     # Could use [namespace code] to generate the callback: 
     # trace add variable alist write [namespace code AlistUpdated] 
     # but that feels like overkill in this case 
     button .b -text update -command PreGen::Update 
     grid ... 
    } 

    proc AlistUpdated {args} { 
     # We just ignore the arguments; don't need them here 
     variable alist 
     .c configure -values $alist 
    } 

    proc Update ... 
} 

当然,如果你永远只能在该命名空间内的程序设置变量,你可以只调用.c configure -values在恰当的时间直接。这是我真正推荐你做的。

+0

非常感谢。这真的让我学到了很多! – Jimmy