[R-gui] pure tcl/tk to R-tcltk / labeled frame and image

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Jan 13 11:56:54 CET 2005


Thomas Unternaehrer <uth at zhwin.ch> writes:

> Dear Peter Dalgaard
....
> >The problem is a generic one: If a Tcl extension package generates a
> >widget with internal subwidgets, these are not readily available to
> >the R side of the interface. .Tk.subwin is not going to help because
> >that invents a *new* name.
> >
> >
> Ok, this is the same "problem" in all of that Megawidgets, isn't it?

A common problem, certainly. Not sure it is in all of them.
 
> > .Tk.subwin <-
> >function (parent, name=evalq(num.subwin <- num.subwin + 1, parent$env))
> >{
> >    ID <- paste(parent$ID, name,
> >        parent$env), sep = ".")
> >    win <- .Tk.newwin(ID)
> >    assign(ID, win, envir = parent$env)
> >    assign("parent", parent, envir = win$env)
> >    win
> >}
> >
> >so that .base.labelname.cs <- .Tk.subwin(.base.labelname, "cs") would
> > work.
> If I use your function I get the following error:
> 
> ....
> .Tk.subwin2 <- function (parent, name=evalq(num.subwin <- num.subwin + 1,
>                                    parent$env)) {
>   ID <- paste(c(parent$ID, name, parent$env), sep = ".")
                                   ^^^^^^^^^^

Oops, that's not right. Do what I mean, not what I say...

Should be

 ID <- paste(parent$ID, name, sep = ".")


>   win <- .Tk.newwin(ID)
>   assign(ID, win, envir = parent$env)
>   assign("parent", parent, envir = win$env)
>   win
> }
> 
> .base.labelframe.cs <- .Tk.subwin2(.base.labelframe, "cs")
> tclRequire("Img")
> t.Logo <- tclVar()
> tcl("image", "create", "photo", t.Logo, file = "logo.jpg")
> tkpack(tklabel(.base.labelframe.cs, image = t.Logo, bg = "white"),
>        fill = "both", expand = TRUE, padx = 5, pady = 5)
> 
> Error in structure(.External("dotTclObjv", objv, PACKAGE = "tcltk"),
> class = "tclObj") :
>     [tcl] bad window path name ".1.1.1 cs.1 <environment>".
> 
> 
> I've seen that you use the same trick in the tabbed notebook example.
> Maybe I have to study that part again.

Mmm, I recall discussing that, but not putting up a complete example.
Might have been James Wettenhall. In which context is that?
 
> >(1) There's really no need to use Tcl style names like
> >    .base.labelframe in R. In fact the R interface is designed to
> >    enable you to avoid that.
> >
> Yes I know, but the name .base.labelframe.image tells me that image
> (or what ever) is on top of base and labelframe.
> I find it just usefull.

OK. As long as it works... If you get 10 levels deep you get in
trouble though, because R doesn't have Tcl's macro-like $win.sub
expansion (with $win expanding to a 9 level window name).
 
> Just to understand what happend I've tried these two examples
> 
> ## 1
> .base <- tktoplevel()
> tkwm.title(.base, "Labelframe")
> tkpack(.base.labelframe <- tkwidget(.base, "iwidgets::labeledframe",
>                                     labelpos = "nw",
>                                     labeltext = "Labelframe"),
>        fill = "both", expand = TRUE)
> 
> cs <- tclVar()
> tcl("set", cs, paste("[", .Tk.ID(.base.labelframe), "childsite]"))
> t.Logo <- tclVar()
> tcl("image", "create", "photo", t.Logo, file = "logo.jpg")
> .Tcl(paste("label $cs.l -image ", .Tk.ID(t.Logo)))
> 
> [tcl] can't read "cs": no such variable.
> 
> This is what you said above I think. The variable "cs" can't be passed
> from R to Tcl and back.

It's not quite the same, the above relates to variable names, which
will be "::RTcl1","::RTcl2" on the Tcl side. I was talking about
window names, but the issue is the same; there is no obvious relation
between the names that R knows and the ones that Tcl knows. 

 
>  ## 2    ## what I can't understand is that
> .Tk.ID(cs) ## is NULL
> 
> If I give it an ID by hand it gives me the following:
> ...
> cs$ID <- ".2.1.cs"
> .Tcl(paste("label $",.Tk.ID(cs),".l -image ", .Tk.ID(t.Logo), sep = ""))
> 
> bad window path name "$.2.1.cs"
> 
> That it does not work is not really surprising, but there is maybe
> more than just set the path name correctly, isn't it?

I don't think you want the "$" in there... 

If cs is still meant as a variable here, then you need to understand
the difference between tclVar and tkwin. If you want cs to be a Tcl
variable containing the name of a window, you would have

tclvalue(cs) <- ".2.1.cs"

then you could get the name of cs as 

name <- as.character(cs) 

(although I'm not really happy about that construction)

and then do this sort of thing:

.Tcl(paste("label $", name, ".l -image ", .Tk.ID(t.Logo), sep = "")

-- for instructional use only, of course. I don't think it has a place
   in good programming practice!

-- 
   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