2011-10-17 3 views
0

브렌트 웰치 (Brent Welch)의 책에서 코드를 수정하여 listbox을 사용하여 내 entry 상자를 채울 수 있도록했습니다. 아래에 나열된 코드를 개선 할 수 있습니까? 또한이 코드를 위젯으로 변환하여 코드 기반을 사용하는 모든 사용자가 재사용 할 수있게하려고합니다. 이것을 재사용 가능한 위젯으로 변환하려면 무엇이 필요합니까?Tcl/Tk : entry + listbox : 위젯으로 변환하는 방법?

코드는 아래에 나열되지 않은 :

#---------------------------------------------- 
# Code adapted from Brent Welch's book 
#---------------------------------------------- 
proc scrolled_listbox { f args } { 
    frame $f 

    listbox $f.list 
    eval {$f.list configure} $args 

    scrollbar $f.xscroll -orient horizontal \ 
    -command [list $f.list xview] 
    scrollbar $f.yscroll -orient vertical \ 
    -command [list $f.list yview] 

    grid $f.list -sticky news 
    grid rowconfigure $f 0 -weight 1 
    grid columnconfigure $f 0 -weight 1 

    return $f.list 
} 

proc listbox_transfer_select {src dst} { 
    set select [$src curselection] 

    foreach i $select { 
    set elements [$dst get 0 end] 
    set e [$src get $i] 

    # insert only if the new element e is not present 
    if {$e ni $elements} { 
     $dst insert end $e 
    } 
    } 
} 

proc listbox_delete_select {dst} { 
    foreach i [lsort -integer -decreasing [$dst curselection]] { 
    $dst delete $i 
    } 
} 

proc onOk {picked parent window} { 
    set elements [$picked get 0 end] 
    $parent.e delete 0 end 
    $parent.e insert insert $elements 
    destroy $window 
} 

#---------------------------------------------- 
# Put everything together 
#---------------------------------------------- 

proc list_select { parent values } { 
# Create two lists side by side 
    set choiceWindow ${parent}.choices 
    toplevel $choiceWindow 

    set main [frame $choiceWindow.f1] 
    set choices [scrolled_listbox $main.choices \ 
    -selectmode extended -width 20 -height 5 ] 
    set picked [scrolled_listbox $main.picked \ 
    -selectmode extended -width 20 -height 5] 

    set inter [frame $main.inter -width 20 -height 5] 
    button $inter.b1 -width 10 -text ">>" 
    pack $inter.b1 -side top 
    button $inter.b2 -width 10 -text "<<" 
    pack $inter.b2 -side bottom 
    pack $main.choices $inter $main.picked -side left -expand true -fill both 

    set okcancel [frame $choiceWindow.f2] 
    button $okcancel.ok -text "OK" -command [list onOk $picked $parent $choiceWindow] 
    button $okcancel.cancel -text "Cancel" -command [list destroy $choiceWindow] 
    grid $okcancel.ok $okcancel.cancel 
    pack $main $okcancel -side top -anchor e 

# Selecting in choices moves items into picked 
    bind $inter.b1 <ButtonRelease-1> [list listbox_transfer_select $choices $picked] 

# Selecting in picked deletes items 
    bind $inter.b2 <ButtonRelease-1> [list listbox_delete_select $picked] 

# ok 
    bind $choiceWindow <Return> [list onOk $picked $parent $choiceWindow] 

# cancel 
    bind $choiceWindow <Escape> [list destroy $choiceWindow] 

# Insert all the choices 
    foreach x $values { 
    $choices insert end $x 
    } 
} 

proc my_entry_list { parent options } { 
    frame $parent 
    label $parent.l -text "Fruits:" 
    entry $parent.e -width 15 -textvariable result -relief sunken 
    button $parent.b -text ... -command [list list_select $parent $options] 
    pack $parent.l $parent.e $parent.b -side left 
} 

#---------------------------------------------- 
# main 
#---------------------------------------------- 
set options { grapes mangos peaches pears oranges berries } 
my_entry_list .mel $options 
pack .mel 

답변

0

(아래 8.4에서 유지해야하는 경우) TK에 8.5에서 ttk::combobox 위젯 또는 BWidget에서 ComboBox 위젯을 사용하는 방법을?

+0

'ttk :: combobox'는 단 하나의 선택 만 할 수있게합니다. 여기서는 여러 선택 항목을 선택해야합니다. – Anand

+2

@Anand : 여러 가지를 선택한다는 것은 중요합니다. 전체 인터페이스 디자인에 근본적인 영향을주기 때문입니다. 아아, 코더와 사용자 모두에게 어려운 일이 있습니다 ... –