[R] help: how to use tkevent.generate(...)

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Jul 12 13:41:02 CEST 2005


wu sz <r.shengzhe at gmail.com> writes:

> Hello,
> 
> I use package "tcltk" to do some GUI programming, and want to find a
> function which can do the operation "click a button", just like using
> a mouse to click. If tkevent.generate can do that? I tried it as
> below, but failed. Please give me a hint!
> 
> tt <- tktoplevel()
> tkwm.title(tt,"Simple Dialog")
> 
> onOK <- function(){print("OK")}
> onCancel <- function(){print("Cancel")}
> OK.but <- tkbutton(tt, text="  OK  ", command=onOK)
> Cancel.but <- tkbutton(tt, text="Cancel",command=onCancel)
> tkgrid(OK.but,Cancel.but)
> 
> tkevent.generate(tt, onOK)

That would never work: At the very least you need to send *events* and
to the right widget, not the toplevel. If you just wanted to run onOK,
there's an easier way: onOK(), or generically:

  tcl("eval", tkcget(OK.but, command=NULL))

to run the command associated with OK.but (whether it is an R callback
or not).

The right way to trigger a button press event turns out to be a bit
tricky, but Googling about (for the tcl/tk counterpart "event
generate") found the solution: You need not one, but three events:
Entering the button, pressing the mouse button, releasing the mouse
button, i.e.:

tkevent.generate(OK.but,"<Enter>")
tkevent.generate(OK.but,"<1>")
tkevent.generate(OK.but,"<ButtonRelease-1>")


for (e in c("<Enter>","<1>","<ButtonRelease-1>"))
   tkevent.generate(OK.but,e)






-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list