combobox - Tcl/Tk LstBox width in CombBox -
how can configure listbox component width in combobox, fit longest entry? widget width (the entry component) can short, i'm looking way configure listbox component wider entry...
this possible, not without bit of hacking ;)
you can find implementation of combobox in combobox.tcl
(in case /usr/share/tcltk/tk8.5/ttk/combobox.tcl
. there see if have combobox
set cb [ttk::combobox .cb -state readonly]
and invoke it, creates following internal widget structure:
$cb.popdown.f.l
popdown
toplevel pops down if click combobox, f
frame, , l
actual listbox contains values. fit longest entry need modify geometry of popdown
toplevel.
we try binding resize script thebuttonpress
event on combobox, however, not work because default bindings handled in following order (output of puts [bindtags $cb]
):
.cb tcombobox .
so first events on widget (.cb
) handled, events on class (tcombobox
), , on toplevel (.
) , events bound widgets. means when click combobox, first our resize script executed, popdown
toplevel exist yet, because created when handling class event.
the solution switch event handling order widget , class:
bindtags $cb [list tcombobox $cb . all]
now should working! below minimal proof of concept:
package require tk wm title . "combobox listbox resize" wm geometry . "150x40" grid columnconfigure . 0 -weight 1 set cb [ttk::combobox .cb -width 20 -state readonly] grid $cb -row 1 -column 0 set cmd "$cb configure -values {abarsdhresntdaenstdsnthret erstihre reshterhstierht}" $cb configure -postcommand $cmd bind $cb <buttonpress> changegeompopdown bindtags $cb [list tcombobox $cb . all] proc changegeompopdown { } { global cb scan [wm geometry $cb.popdown] "%dx%d+%d+%d" w h x y wm geometry $cb.popdown "300x${h}+${x}+${y}" }
to desired width need ask font of listbox [1]:
$cb.popdown.f.l cget -font
then use font measure
command ([2]) on every string determine pixels need fit entries. don't forget add bit of padding better , make sure scrollbar doesn't overlap text.
nb: avoid kind of nasty stuff please make sure internal widget structure in place before try modify it, implementation of ttk::combobox
might change , program won't work anymore!
[1] http://www.tcl.tk/man/tcl8.5/tkcmd/listbox.htm#m16
[2] http://www.tcl.tk/man/tcl8.5/tkcmd/font.htm#m10
Comments
Post a Comment