From ripley at stats.ox.ac.uk  Mon Feb 28 15:24:55 2005
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon Feb 28 15:25:06 2005
Subject: [Rd] Getting width of Tk text widget via tcltk
In-Reply-To: <20050228140503.YGVI1919.tomts22-srv.bellnexxia.net@JohnDesktop8300>
References: <20050228140503.YGVI1919.tomts22-srv.bellnexxia.net@JohnDesktop8300>
Message-ID: <Pine.LNX.4.61.0502281415380.24599@gannet.stats>

On Mon, 28 Feb 2005, John Fox wrote:

> Dear list members,
>
> Is it possible via a suitable tcltk command to get the *current* width, in
> characters, of a Tk text widget that has been resized with the mouse? In the
> following code, the reported width of the text widget doesn't change, even
> though it has been resized. I can, however, get the current width in pixels:
>
>> library(tcltk)
>> top <- tktoplevel()
>> textWindow <- tktext(top, bg="white", height=20, width=80, wrap="none")
>> tkgrid(textWindow, sticky="news")
> <Tcl>
>> tkgrid.rowconfigure(top, 0, weight=1)
> <Tcl>
>> tkgrid.columnconfigure(top, 0, weight=1)
> <Tcl>
>> tkcget(textWindow, width=NULL)
> <Tcl> 80
>> tkwinfo("width", textWindow$ID)
> <Tcl> 486
>
>> # resize window with mouse
>> tkcget(textWindow, width=NULL)
> <Tcl> 80
>> tkwinfo("width", textWindow$ID)
> <Tcl> 743
>
> I could convert pixels to characters, but wonder whether I can get the
> latter directly.

I suspect you may have some difficulty with the latter, even in a 
monospace font.  Notice that 486 is not a multiple of 80, and if that is 
actually 81, 743 is not a multiple of 6.

I was trying to do this with heights for a listbox a few days ago, and it 
seems that the line spacing is actually 1 pixel greater than is reported. 
Since you can resize to a non-integer number of lines I don't believe (and 
I tried to read the source code) that Tcl/Tk works internally with 
characters.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] Getting width of Tk text widget via tcltk
Prof Brian Ripley