[R] tcltk: click and return table cell index

Dalthorp, Daniel ddalthorp at usgs.gov
Mon May 2 18:20:09 CEST 2016


Thanks, John.

The trouble with that solution is that it gives the index for where the
cursor was before clicking rather than the cell that was clicked. The
solution is that the <Button-1> binding gives the x, y pixel coordinates of
the click to the callback, and the pixel coordinates can be translated to
cell index via:

tkindex(table1, as.tclObj(paste0("@",x, ",", y)))

Or, showing the whole example:

# create table
tt<-tktoplevel()
table1<-tkwidget(tt,"table", rows=3,cols=3)
tkgrid(table1)

# bind the mouse click to printing out cell index:
tkbind(table1, "<Button-1>", function(x, y) print(tcl(table1, "index",
as.tclObj(paste0("@",x, ",", y)))))

Thanks again!

-Dan

On Sat, Apr 30, 2016 at 6:43 AM, Fox, John <jfox at mcmaster.ca> wrote:

> Dear Daniel,
>
> Try
>
> tkbind(table1, "<Button-1>", function(){
>      res <- try(tclvalue(tkindex(table1, "active")), silent=TRUE)
>    if (inherits(res, "try-error")) print (NULL)
>    else print(res)
> })
>
> I put in the calls to print() so that you could see how it works.
>
> I hope this helps,
>  John
>
> -----------------------------
> John Fox, Professor
> McMaster University
> Hamilton, Ontario
> Canada L8S 4M4
> Web: socserv.mcmaster.ca/jfox
>
>
> > -----Original Message-----
> > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of
> Dalthorp,
> > Daniel
> > Sent: April 29, 2016 1:42 PM
> > To: r-help at R-project.org (r-help at r-project.org) <r-help at r-project.org>
> > Subject: [R] tcltk: click and return table cell index
> >
> > I'm struggling mightily with what should be a simple task...when a user
> clicks
> > on a cell in a tcltk table widget, I need to know which cell was clicked.
> >
> > One idea that gives a cryptic error:
> > tkbind(table1, "<Button-1>", function(x, y){
> >   tcl(table1, "index", x, y)
> > }
> >
> > # x, y give pixel coordinates; "index" should give cell coordinates, but
> format
> > must be correct
> >
> > I get an error message:
> >
> > wrong # args: should be ".25.1 index <index> ?row|col?".
> >
> > To which I respond, "Yes, I know I have the format wrong, but how can I
> make
> > sense of THAT?"
> >
> > Does anyone know a simple fix?
> >
> > Much appreciated!
> >
> > -Dan
> >
> > --
> > Dan Dalthorp, PhD
> > USGS Forest and Rangeland Ecosystem Science Center Forest Sciences Lab,
> Rm
> > 189
> > 3200 SW Jefferson Way
> > Corvallis, OR 97331
> > ph: 541-750-0953
> > ddalthorp at usgs.gov
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-
> > guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>



-- 
Dan Dalthorp, PhD
USGS Forest and Rangeland Ecosystem Science Center
Forest Sciences Lab, Rm 189
3200 SW Jefferson Way
Corvallis, OR 97331
ph: 541-750-0953
ddalthorp at usgs.gov

	[[alternative HTML version deleted]]



More information about the R-help mailing list