選擇窗口小部件用于選擇在一個(gè)Tk應(yīng)用程序不同的選項(xiàng)??捎玫倪x擇窗口小部件的列表,如下所示。
| 1 | Radiobutton | 窗口小部件具有一組開/關(guān)按鈕和標(biāo)簽,其中一個(gè)可被選擇 |
| 2 | Checkbutton | 窗口小部件具有一組開/關(guān)按鈕和標(biāo)簽,其中許多可被選擇。 |
| 3 | Menu | 窗口小部件為菜單項(xiàng)充當(dāng)支架。 |
| 4 | Listbox | 窗口小部件,顯示一個(gè)或多個(gè)單元格,其中的列表可以被選擇。 |
一個(gè)簡(jiǎn)單的Tk選擇部件使用的示例如下所示。
#!/usr/bin/wish grid [frame .gender ] grid [label .label1 -text "Male" -textvariable myLabel1 ] grid [radiobutton .gender.maleBtn -text "Male" -variable gender -value "Male" -command "set myLabel1 Male"] -row 1 -column 2 grid [radiobutton .gender.femaleBtn -text "Female" -variable gender -value "Female" -command "set myLabel1 Female"] -row 1 -column 3 .gender.maleBtn select grid [label .myLabel2 -text "Range 1 not selected" -textvariable myLabelValue2 ] grid [checkbutton .chk1 -text "Range 1" -variable occupied1 -command {if {$occupied1 } { set myLabelValue2 {Range 1 selected} } else { set myLabelValue2 {Range 1 not selected} } }] proc setLabel {text} { .label configure -text $text }
當(dāng)我們運(yùn)行上面的程序,會(huì)得到下面的輸出。