Hello, I am using R 2.11.1 under Windows XP.
I would like to know if its possible to use a function with arguments as a
command in tcl tk. For example
require(tcltk)
PressedOK <- function()
{
tkmessageBox(message="You pressed OK!")
}
tt <- tktoplevel()
OK.but <- tkbutton(tt,text="OK",command=PressedOK)
tkgrid(OK.but)
tkfocus(tt)
the function PressedOK has no arguments and its use in
"tkbutton(tt,text="OK",command=PressedOK)" is without any
brackets.
Unfortunately I would like to use several buttons and therefor define the
corresponding functions to it.
For example
require(tcltk)
OK.but <- NULL
PressedOK <- function(i)
{
tkmessageBox(message=paste("You pressed OK!",i,sep=""))
}
tt <- tktoplevel()
for(i in seq(3)){
OK.but[[i]] <-
tkbutton(tt,text="OK",command=function()PressedOK(i))
tkgrid(OK.but[[i]])
}
tkfocus(tt)
In this case, all buttons have the command of PressedOK(i) and therefore its
is always written "You pressed OK! 3" even if you push button 1. I
think
this is due to the fact that the PressedOK(3) was the last call of the
function, but I don't understand why all the other buttons have now a
different command. Any idea?
Thanks
--
View this message in context:
http://r.789695.n4.nabble.com/tcl-tk-command-function-with-arguments-tp4416470p4416470.html
Sent from the R help mailing list archive at Nabble.com.
On Fri, Feb 24, 2012 at 12:58 AM, Alexander <juschitz_alexander at yahoo.de> wrote:> I would like to know if its possible to use a function with arguments as a > command in tcl tk.Yes <snip> I think> this is due to the fact that the PressedOK(3) was the last call of the > function, but I don't understand why all the other buttons have now a > different command. Any idea? >Because in for loop i is overwritten every time - as you said i=3 is the last command. Replacing for with sapply should fix it: require(tcltk) OK.but <- NULL PressedOK <- function(i) { tkmessageBox(message=paste("You pressed OK!",i,sep="")) } tt <- tktoplevel() sapply(1:3,function(i){ OK.but[[i]] <- tkbutton(tt,text="OK",command=function()PressedOK(i)) tkgrid(OK.but[[i]]) }) tkfocus(tt) Cheers, Elai> Thanks > > > -- > View this message in context: http://r.789695.n4.nabble.com/tcl-tk-command-function-with-arguments-tp4416470p4416470.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.