Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

get width of variable-width font text in pixels for treeview column resize

626 views
Skip to first unread message

anne

unread,
Jan 24, 2012, 3:28:08 PM1/24/12
to
I have a treeview object with a column that could have a lot of text in it. I'd like to resize the column to be as big as the largest item. With a horizontal scrollbar, the user could then see all of the text. However, I can't figure out how to get the width of the text that was added to the item. So I can set the size to be some wild guess based on the number of characters, but my font is variable-width, so this is likely to be very wrong.

To help explain my dilemma, here's an example. The following script creates a treeview widget and a horizontal scrollbar. It then populates the treeview with increasingly long strings. At startup, the strings end at the end of the window, and there is no way to scroll. The user could manually resize the column beyond the window width, and then the scrollbar is enabled. But I'd like this resize to happen automatically, and to the exact width desired.


grid [ttk::treeview .l -xscrollcommand ".s set" -height 5] -column 0 -row 0 -sticky nwes
grid [ttk::scrollbar .s -command ".l xview" -orient horizontal] -column 0 -row 1 -sticky we

grid columnconfigure . 0 -weight 1; grid rowconfigure . 0 -weight 1
for {set i 0} {$i<5} {incr i} {
set x [string repeat "xxxxxxxxx" $i]
set id [.l insert {} end -id $i -text "Line $i of 5: $x"]
# here I'd like to resize the column if necessary! but to what width?
}

Am I missing an easy way to get the width of text in a given font?

Aric Bills

unread,
Jan 24, 2012, 4:28:47 PM1/24/12
to
The easy way to get the width of text in a given font is [font measure
$font -displayof $window $text]. The -displayof $window part is
optional.

Jeff Godfrey

unread,
Jan 24, 2012, 4:28:52 PM1/24/12
to
On 1/24/2012 2:28 PM, anne wrote:
> Am I missing an easy way to get the width of text in a given font?

I'd guess you're looking for "font measure".

Jeff

anne

unread,
Jan 25, 2012, 8:49:03 AM1/25/12
to
Thanks everyone. That is helpful. Not sure why I missed it before. Now to retrieve the font of the text inside the treeview widget, the only way I can find to do that is to tag an item, and then query that tag for the font. Is there a more direct way?

anne

unread,
Jan 25, 2012, 9:19:11 AM1/25/12
to
One more question -- when I resize the column, the parent frame/window resizes to the width of the entire treeview, even when it's, say, 5000 pixels. Is there a way to force the parent not to resize?

Aric Bills

unread,
Jan 25, 2012, 11:20:14 AM1/25/12
to
On Jan 25, 7:19 am, anne <adudfi...@gmail.com> wrote:
> One more question -- when I resize the column, the parent frame/window resizes to the width of the entire treeview, even when it's, say, 5000 pixels. Is there a way to force the parent not to resize?

For the grid geometry manager, use [grid propagate $parent 0].

Aric Bills

unread,
Jan 25, 2012, 11:19:32 AM1/25/12
to
On Jan 25, 6:49 am, anne <adudfi...@gmail.com> wrote:
> Thanks everyone. That is helpful. Not sure why I missed it before. Now to retrieve the font of the text inside the treeview widget, the only way I can find to do that is to tag an item, and then query that tag for the font. Is there a more direct way?

By default, the font will be TkDefaultFont, a named font created
automatically by Tk. I don't have an easy way to check whether that's
the font in use for a particular treeview item, but if you don't allow
users the option to change fonts in the treeview widget, that's the
font you'll end up with.

anne

unread,
Jan 25, 2012, 1:33:18 PM1/25/12
to
Thanks! It looks like my problem was that I was populating the widget before the tk eventloop ran, so it was using the inner widgets' size to size the window. If I call [update] to process events before populating the widget, the window is sized appropriately.

Donal K. Fellows

unread,
Jan 26, 2012, 6:23:17 AM1/26/12
to
You'll get a similar effect without the reentrant event handling hazards
by doing the populating on a <Configure> event, which is sent when the
size of the outer widget is decided on. (You'll want to deregister the
event handler at that point, unless you want to find out every time some
geometry management is done.)

Donal.
0 new messages