[Rd] Getting width of Tk text widget via tcltk

John Fox jfox at mcmaster.ca
Mon Feb 28 15:41:10 CET 2005


Dear Brian,

As you guessed, I am using a monospaced font. I'll try the equivalent of
floor(743/(486/(80 + 1))) and see whether it's reliable. In particular,
thanks for the tip about the additional character (perhaps due to Tcl
0-based indexing?) -- a bit of experimentation shows that it's consistently
true.

Regards,
 John

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
-------------------------------- 

> -----Original Message-----
> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] 
> Sent: Monday, February 28, 2005 9:25 AM
> To: John Fox
> Cc: r-devel at stat.math.ethz.ch
> Subject: Re: [Rd] Getting width of Tk text widget via tcltk
> 
> 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 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