[R] [tcltk] binding two functions to one event

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Tue Jan 21 11:39:35 CET 2025


This is in the obscure corners of the tcltk interface, as I am sure you already realized...

I'm not even sure whether the "+" in bind .... +script is syntactically an operator or part of the command.

However, it is usually not right to define named functions on the Tcl side and mix them with the ones R/Tcl interface generate.

So I am thinking that if tkbind('all','<<EVENT>>',evresp1) works, then to add evresp2, you just need to get a "+" into whatever the evresp2 gets converted to.

And...

> tkbind('all','<<EVENT>>',evresp1)
<Tcl>  
> tkbind('all','<<EVENT>>',paste0("+", .Tcl.callback(evresp2)))
<Tcl>  
> tkevent.generate(wtop,'<<EVENT>>',data='abcdef')
evresp1 abcdef 
evresp2 abcdef 
<Tcl>  

> tkbind('all','<<EVENT>>')
<Tcl> R_call 0x7f9769c764b0 %d
R_call 0x7f9769c79978 %d 

-pd

PS. To answer my own question: yes, the "+" is part of the command because this does not work:

> tkbind('all','<<EVENT>>',"+", evresp2)
Error in structure(.External(.C_dotTclObjv, objv), class = "tclObj") : 
  [tcl] wrong # args: should be "bind window ?pattern? ?command?".


> On 20 Jan 2025, at 02:51 , ressw using meer.net wrote:
> 
> 
> 
> Here is some tcl/tk code I am trying to emulate in R.
> Paste the following into wish
> 
> ### begin tcl code
> toplevel .wtop
> proc evresp1 { data } { puts "evresp1 $data" }
> proc evresp2 { data } { puts "evresp2 $data" }
> bind all <<EVENT>> "evresp1 %d"
> 
> ### end tcl code
> 
> Now paste
> 
> event generate .wtop <<EVENT>> -data abcdef
> 
> I see the output
> evresp1 abcdef
> evresp2 abcdef
> 
> Both evresp1 and evresp2 got called in response to the event and both
> got the data.
> 
> Then paste the following into an R session
> 
> ### begin R code
> library(tcltk)
> catn = function(...) { cat(...,'\n') }
> wtop = tktoplevel()
> evresp1 = function(d) { catn('evresp1',d) }
> evresp2 = function(d) { catn('evresp2',d) }
> tkbind('all','<<EVENT>>',evresp1)
> .Tcl(paste('proc evresp2 { d } { ', .Tcl.callback(evresp2),'}'))
> .Tcl('bind all <<EVENT>> "+evresp2 %d"')
> ### end R code
> 
> The .Tcl() calls are necessary (as far as I can tell) to get the
> effect of 'bind all <<EVENT>> "+evresp2 %d"' ,i.e. appending evresp2
> 
> Now paste
> 
> 
> I see output
> evresp1 abcdef
> evresp2 %d
> 
> Both evresp1 and evresp2 got called but only evresp1 got the data right.
> 
> How can I fix this?
> 
>> sessionInfo
> R version 4.3.0 (2023-04-21)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Ubuntu 20.04.6 LTS
> attached base packages:
> [1] tcltk     stats     graphics  grDevices utils     datasets  methods
> [8] base
> loaded via a namespace (and not attached):
> [1] compiler_4.3.0
>> tclVersion()
> [1] "8.6.10"
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com



More information about the R-help mailing list