2012-06-20 51 views
0

我有一個代碼,其中標籤是從列表中自動創建的。我想將標籤更改爲按鈕,並根據其類型和一些參數爲其中的每個命令分配命令。我試過這樣做:自動分配命令到Tcl/Tk中的按鈕

proc create_prop_menu {{inst ""}} { 

    set wdth 90 
    set w 20 

    if {$inst == ""} { 
     puts "please select a block first , then Propertires info " 
    return 
    } 


    set cell [get_cell $inst] 
    set cell_name [cell_get_name $cell] 
    set inst_name [inst_get_name $inst] 
    catch {destroy .prop_menu} 
    toplevel .prop_menu 
    # Main Frame 
    frame  .prop_menu.main_frame -relief "groove" -bd 2 
    pack  .prop_menu.main_frame 

    set lbl [label .prop_menu.main_frame.cell_name_lbl -relief flat -text "Cell Name:" -width $w] 
    set lbl2 [entry .prop_menu.main_frame.cell_name -relief flat -width $wdth] 
    $lbl2 insert 0 $cell_name 
    $lbl2 configure -state readonly 
    grid $lbl -row 1 -column 1 -sticky nse 
    grid $lbl2 -row 1 -column 2 -sticky nsw 

    set r 2 

    foreach fd [get_all_properties $cell] { 
     set n [property_get_name $fd] 
     if [cequal $n "value"] { 
      set v [get_property $inst value] 
     } else { 
      set v [get_value_on_object $fd [get_id $cell]] 
     } 


     if ![cequal "NONE" $v] { 
      set cmd "" 
      if {[cequal $n "type"]} { 
       set l "Type:" 
       set t $v 
      } elseif {[cequal $n "signals"]} { 
       set l "Signals:" 
       set t [qb_compress_names $v 1] 
       set cmd [list "chose_net_from_prop" $n] 
      } elseif {[cequal $n "clocks"]} { 
       set l "Clocks:" 
       set t [qb_compress_names $v 1] 
       set cmd [list "chose_net_from_prop" $n] 
      } elseif {[cequal $n "ref_supply"]} { 
       set l "Reference Suply:" 
       set t [qb_compress_names $v 1] 
       set cmd [list "chose_net_from_prop" $n] 
      } elseif {[cequal $n "stackup_file"]} { 
       set l "Stackup File:" 
       set t $v 
       set cmd [list "chose_file_from_prop" $n] 
      } elseif {[cequal $n "doe_file"]} { 
       set l "DOE File:" 
       set t $v 
       set cmd [list "chose_file_from_prop" $n] 
      } elseif {[cequal $n "lay_file"]} { 
       set l "LAY File:" 
       set t $v 
       set cmd [list "chose_file_from_prop" $n] 
      } elseif {[cequal $n "prs_file"]} { 
       set l "PRS File:" 
       set t $v 
       set cmd [list "chose_file_from_prop" $n] 
      } elseif {[cequal $n "netlist_file"]} { 
       set l "NetList File:" 
       set t $v 
       set cmd [list "chose_file_from_prop" $n] 
      } elseif {[cequal $n "ckt_file"]} { 
       set l "CKT File:" 
       set t $v 
       set cmd [list "chose_file_from_prop" $n] 
      } elseif {[cequal $n "sparam_file"]} { 
       set l "S-Param File:" 
       set t $v 
       set cmd [list "chose_file_from_prop" $n] 
      } elseif {[cequal $n "type_order"]} { 
       set l "Type Order:" 
       set t $v 
       set cmd "chose_type_order_from_prop" 
      } else { 
       set l "NONE" 
        set t $v 
      } 
      #puts "$n -> $l -> $v" 
      if {![cequal "NONE" $l]} { 
       lappend cmd $inst 
       if [cequal $l "Type:"] { 
        set lbl [label .prop_menu.main_frame.lbl_$n -relief flat -text $l -width $w] 
       } else { 
        set lbl [button .prop_menu.main_frame.lbl_$n -text $l -width $w -command "\{$cmd\}"] 

       } 
       set lbl2 [entry .prop_menu.main_frame.val_$n -relief flat -width $wdth] 
       $lbl2 insert 0 $t 
       $lbl2 configure -state readonly 
       grid $lbl -row $r -column 1 -sticky nse 
       grid $lbl2 -row $r -column 2 -sticky nsw 

       set r [expr $r + 1] 
      } 
     } 
    } 
    set in "Instance_Name" 
    set sep [ttk::separator .prop_menu.main_frame.sep  -orient horizontal] 
    set lbl [label .prop_menu.main_frame.inst_name_lbl -relief flat -text "Instance Name:" -width $w] 
    set lbl2 [entry .prop_menu.main_frame.inst_name  -relief flat -width $wdth] 
    $lbl2 insert 0 $inst_name 
    $lbl2 configure -state readonly 
    grid $sep -row $r -columnspan 2 
    grid $lbl -row [expr $r+1] -column 1 -sticky nse 
    grid $lbl2 -row [expr $r+1] -column 2 -sticky nsw 

    set ok_btn [button .prop_menu.main_frame.ok_b -text "OK" -command {prop_menu_ok_button}] 
    grid $ok_btn -row [expr $r+2] -column 1 -columnspan 2 -sticky nsew 

    grab release . 
    grab set .prop_menu 
    center_the_toplevel .prop_menu 
    bind .prop_menu <Key-Return> {prop_menu_ok_button} 
} 

但它不起作用。它所做的不是使用參數lay_file $inst運行函數chose_file_from_prop,而是嘗試運行一個名爲chose_file_from_prop lay_file $inst的函數,並告訴我它不存在。

有沒有一種方法可以在不爲每個可能的標籤創建不同的功能的情況下克服它,而且不會使$inst成爲全局變量?

回答

1

如果您使用list生成了一條命令,那麼您可以將它與-command一起使用,而無需進一步換行。請勿在其周圍放置{/}


也就是說,是不是這樣的:

set lbl [button .prop_menu.main_frame.lbl_$n -text $l -width $w -command "\{$cmd\}"] 

這樣寫:

set lbl [button .prop_menu.main_frame.lbl_$n -text $l -width $w -command $cmd]