Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox height

Status
Not open for further replies.

fhutt

Programmer
Jul 7, 2006
79
AU
I am trying to create a listbox that has the exact height of a determined number of lines using specified fonts.

I tried to multiply the font size by 1.33 and then devide by 0.72 to get the number of pixels for each line. Unfortunately, this doesn't work correctly. I tried to tweek the numbers above to get it right. It will then be right for a font size but fail for another font sizes.

Can anyone please help?
fhutt
 
According to the option definition, -height on a listbox is in "number of lines". And, indeed, it appears to work that way:
Code:
% pack [listbox .lb -listvariable lst1 -height 4] -side top
() 2 % set lst1 {abc}
abc
() 3 % lappend lst1 222
abc 222
() 4 % lappend lst1 333
abc 222 333
() 5 % lappend lst1 444
abc 222 333 444
() 6 % lappend lst1 555
abc 222 333 444 555
() 7 % .lb configure -height 5
() 8 % .lb configure -font courier
() 9 %

_________________
Bob Rashkin
 
Bong, you're on the ball as usual.
The listbox height option being in characters height means I need to use the pack manager instead of the place manager. It looks like the listbox widthe option is also in characters. This fixed another problem but caused another one.

I am placing the listbox and a scrollbar, if needed in a frame. I need to use the place manager for the frame. The height and width options in the place command for the frame are in pixels. So, I still need to work out the number of pixels for the height and width of the frame to house the listbox and scroll when needed.
Here is the code I've been trying:
# SETUP FRAME AND LISTBOX
frame $top_w.fra26 \
-borderwidth 2 -relief groove
listbox $top_w.fra26.lis32 \
-background $backg -font $w_font -relief flat \
-width $char_width

#Fill listbox with entries
foreach i $list_box {
$top_w.fra26.lis32 insert end $i
}

####Set up Scrollbar
if {$scroll == 1} {
$top_w.fra26.lis32 config \
-yscrollcommand "$top_w.fra26.scroll33 set"
scrollbar $top_w.fra26.scroll33 \
-command "$top_w.fra26.lis32 yview" -borderwidth 0
# set t_width [expr $t_width + 2]
}

###Place box and scroll widgets on screen
place $top_w.fra26 \
-x $fra_h_pos -y $fra_v_pos -width [expr $t_width + ($scroll * 15) + 4] \
-anchor nw -bordermode ignore \
-height $fra_ht
pack $top_w.fra26.lis32 \
-in $top_w.fra26 -anchor nw -expand 0 -fill both -side left
# place $top_w.fra26.lis32 \
-in $top_w.fra26 -x 2 -y 2 -width $t_width -anchor nw \
-bordermode ignore -height $list_ht


if {$scroll == 1} {
pack $top_w.fra26.scroll33 \
-in $top_w.fra26 -anchor w -expand 0 -fill y -side left
# place $top_w.fra26.scroll33 \
-in $top_w.fra26 -x [expr $t_width + 2] -y 2 -width 15 -height $list_ht \
-anchor nw -bordermode ignore
}

The $scroll is 0 if the scrollbar is not required.
the scrollbar is required in case the listbox does not fit in the window.

There are 2 problems. Both relate to pixels versus characters.
1) Because I use the fill option the listbox fills vertically, but since the frame size is wrong the listbox height is also wrong.
2) For some reason the width does not fill, so there is a gap between the right side of the listbox and the frame. Or there is a gap between the scrollbar and the frame.

Hope you can figure out how to make this work.
fhutt
 
In your code:
Code:
pack $top_w.fra26.lis32 \
    -in $top_w.fra26 -anchor nw -expand 0 -fill both -side left
Does setting the -expand option to 1 resolve anything?
 
Thank you PinkeyNBrain.
That fixes the width problem.

Now just the height problem. Somehow I need to find out the height of the listbox in pixels (not lines) before I place the frame.

Can someone let me know how to do that?
Thanks
fhutt
 
I think I found a solution.

Configure the listbox height with:
$top_w.fra26.lis32 configure -height $lines

To get the listbox height in pixels:
set list_ht [winfo reqheight $top_w.fra26.lis32]

By the way, this list_ht includes 6 pixels of padding. So to determine each line height:
set line_ht [expr ($list_ht - 6) / $lines]

I can take it from here. Thank you bong and PinkeyNBrain for all your help.
fhutt
 
With your code: set line_ht [expr ($list_ht - 6) / $lines]
Watch out for different font sizes and screen resolutions. I've got some stuff that works great on 1024x768 and higher. However I also have some (older) coworkers who won't get off of the 800x600 setting and some of the window is truncated at the the edge of the screen. Unless you're using the -font attribute in your definitions, some PCs may drop in a different default.
 
Thank you PinkeyNBrain.
I tried different font sizes and appears to work o/k.
I didn't check for different screen resolutions. I take your point - I think the size in pixels from winfo reqheight would be right, but my present calculations for the padding may be wrong.
Thanks for the warning.
fhutt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top