From jfox at mcmaster.ca  Mon Feb 28 18:01:32 2005
From: jfox at mcmaster.ca (John Fox)
Date: Mon Feb 28 18:01:43 2005
Subject: [Rd] Getting width of Tk text widget via tcltk
In-Reply-To: <x2ekf0hcqn.fsf@biostat.ku.dk>
Message-ID: <20050228170132.MWSF1836.tomts16-srv.bellnexxia.net@JohnDesktop8300>

Dear Peter,

As you can see from the expression, I didn't use integer division, nor did I
round() or floor(). Here's my original example, using a monospaced font:

> library(tcltk)
> top <- tktoplevel()
> textWindow <- tktext(top, bg="white", height=20, width=80, wrap="none",
+     font=tkfont.create(family="courier", size=10))
> 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> 646 
> 
> (as.numeric(tkwinfo("width", textWindow$ID))
+      - 2*as.numeric(tkcget(textWindow, borderwidth=NULL)) - 4) /
+      as.numeric(tkfont.measure(tkcget(textWindow, font=NULL), "0"))
[1] 79.75
> 
> (as.numeric(tkwinfo("width", textWindow$ID))
+      - 2*as.numeric(tkcget(textWindow, borderwidth=NULL)) - 2) /
+      as.numeric(tkfont.measure(tkcget(textWindow, font=NULL), "0"))
[1] 80

I believe that the right answer is 80, and this appears correct visually,
confirmed by typing in the window.

My object was to be able to adjust R output to the width of the widget, and
I can now do this (actually, Brian's approach also worked well enough for
me).

Thank you again for your help.

John

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

> -----Original Message-----
> From: Peter Dalgaard [mailto:p.dalgaard@biostat.ku.dk] 
> Sent: Monday, February 28, 2005 11:27 AM
> To: John Fox
> Cc: 'Peter Dalgaard'; r-devel@stat.math.ethz.ch
> Subject: Re: [Rd] Getting width of Tk text widget via tcltk
> 
> "John Fox" <jfox@mcmaster.ca> writes:
> 
> > Dear Peter,
> > 
> > (as.numeric(tkwinfo("width", .output$ID))
> >     - 2*as.numeric(tkcget(.output, borderwidth=NULL)) - 2)/
> >     as.numeric(tkfont.measure(tkcget(.output, font=NULL), "0"))
> > 
> > (for the text widget .output) appears to do the trick (note, 
> > subtracting 2 rather than 4).
> 
> Hmm, that's odd. I needed the 4. Beware that Tcl does integer 
> division (%/%). Did you round() or floor() the result? 
> 
> -- 
>    O__  ---- Peter Dalgaard             Blegdamsvej 3  
>   c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
>  (*) \(*) -- University of Copenhagen   Denmark      Ph: 
> (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: 
> (+45) 35327907


[Rd] Getting width of Tk text widget via tcltk
John Fox