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

ressw m@iii@g oii meer@@et ressw m@iii@g oii meer@@et
Mon Jan 20 02:51:47 CET 2025



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"
bind all <<EVENT>> "+evresp2 %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
tkevent.generate(wtop,'<<EVENT>>',data='abcdef')

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"



More information about the R-help mailing list