自动为Tcl / Tk中的按钮分配命令

时间:2012-06-20 08:06:45

标签: 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}
}

但它不起作用。它所做的不是运行带有参数chose_file_from_prop的函数lay_file $inst,而是尝试运行一个名为chose_file_from_prop lay_file $inst的函数,并告诉我它不存在。

有没有办法克服它而不为每个可能的标签创建不同的函数而不将$inst变成全局变量?

1 个答案:

答案 0 :(得分: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]