AW: [R-gui] tkbind-question

"Unternährer Thomas, uth" uth at zhwin.ch
Fri Jan 16 14:20:29 MET 2004


Thanks a lot for your answer...

I know the command tkcmd, but it's not really clear to me what exactly is the benefit in using tkcmd instead of 
.Tcl(paste(...)). If I use the .Tcl-version I can use exactly the tcl-syntax, what sometimes is easier.

Are there some real benefits in using tkcmd()?



-----Ursprüngliche Nachricht-----
Von: Peter Dalgaard [mailto:p.dalgaard at biostat.ku.dk] 
Gesendet: Freitag, 16. Januar 2004 12:29
An: Unternährer Thomas, uth
Cc: r-sig-gui at stat.math.ethz.ch
Betreff: Re: [R-gui] tkbind-question


"Unternährer Thomas, uth" <uth at zhwin.ch> writes:

> Hi all,
> 
> I try to understand how to mix R- and Tcl-commands.
> I have three versions of key-binding. The first two versions are clear 
> to me.
> The third version is a bit confusing.
> 
> ##====================================================================
> ==========
> library(tcltk)
> 
> print.textwidget <- function(){
>   print(tclvalue(tkget(.t, "0.0", "end")))
> }
> 
> ## version 1
> . <- tktoplevel()
> .t <- tktext(.)
> tkpack(.t)
> tkbind(.t, "<Control-Key-r>", print.textwidget)
> 
> ## version 2
> . <- tktoplevel()
> .t <- .Tk.subwin(.)
> .Tcl(paste("pack [text", .Tk.ID(.t), "]"))
> tkbind(.t, "<Control-Key-r>", print.textwidget)
> 
> ## version 3
> Path <- "PATH TO ctext.tcl"
> .Tcl(paste("source", paste(Path, "/ctext.tcl", sep = "")))
> . <- tktoplevel()
> tkpack(.t <- tkwidget(., "ctext"))
> tkbind(., "<Control-Key-r>", print.textwidget) 
> ##====================================================================
> ==========
> 
> In the first two versions I bind the command on the .t (the 
> text-widget). In the third version I must bind it on the . (my base 
> window)?! Why? I assume that it is because ctext is a widget. Somehow 
> different to a text-widget (tktext gives me a widget too, a 
> text-widget, isn't it?).
> 
> Can anybody give me an explanation to my question?

[.Tcl(paste(....)) is almost always better written with tkcmd(...) and you can get bitten by embedded spaces if you don't]

The brief answer is that it is because "ctext" is a non-standard widget and you can't bind events to it (in Tcl either). Looking at the code, it looks like the widget creation command creates a frame containing two text widgets, $win.l and $win.t. Presumably, you cannot bind keyboard events to $win itself (please check Tcl documentation for confirmation - I haven't). So you should probably use something like

tkbind(paste(.Tk.ID,"t",sep=".") ,"<Control-Key-r>", print.textwidget))

(This type of megawidget issue is not unique; similar things happen with tabbed notebook widgets and the like. One of the remaining issues with the R/tcltk interface is that there is no really clean way of making subwidgets created in Tcl accessible from R. Probably not too hard to do, though.)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907



More information about the R-SIG-GUI mailing list