2017-08-28 115 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