[Rd] Getting width of Tk text widget via tcltk

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Feb 28 15:24:55 CET 2005

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 

Brian D. Ripley,                  ripley at 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

More information about the R-devel mailing list