1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
namespace eval selectionwindow {
variable selectionvalue
variable selectionwindow
}
proc selectionwindow::selectfromlist { window title selectionlist args } {
variable selectionvalue
variable selectionwindow
if { [winfo exists $window] } {
raise $window;
return [lindex $selectionlist 0]
}
set selectionwindow $window
toplevel $selectionwindow
wm geometry $selectionwindow +200+200
focus -force $selectionwindow
wm title $selectionwindow $title
set maxstrlength [expr [string length $title]+12]
if { [llength $selectionlist]==0 } { destroy $selectionwindow; return {} }
foreach elem $selectionlist {
if { [string length $elem]>$maxstrlength } {
set maxstrlength [string length $elem]
}
}
scrollbar $selectionwindow.scroll -command "$selectionwindow.listbox yview"
eval "listbox $selectionwindow.listbox -yscroll \"$selectionwindow.scroll set\" \
-width $maxstrlength -height 10 -setgrid 1 $args"
pack $selectionwindow.listbox $selectionwindow.scroll -side left -fill y -expand 1
foreach elem $selectionlist {
$selectionwindow.listbox insert end $elem
}
bind $selectionwindow.listbox <Double-1> {
namespace eval selectionwindow {
set selectionvalue [selection get]
destroy $selectionwindow
}
}
tkwait window $selectionwindow
if { [info exists selectionvalue] } {
return $selectionvalue
} else {
if { [llength $selectionlist] != 0 } {
return [lindex $selectionlist 0]
} else {
return ""
}
}
}
# puts [selectionwindow::selectfromlist .demo "Whle Frucht" { Apfel Birne Zitrone dsfsdfdsfdsfdsfsdfds}]
|