简体   繁体   中英

R GUI with tcltk package: tkget() throws error when reading text property: “[tcl] bad option ”get“: must be cget or configure”

When I run a pure Tcl/Tk script I can read the text property of a label with the cget command:

$ wish
% label .lb -text "Read this"
pack .lb
.lb cget -text
.lb
% % Read this

If I try to do this in R using the tkget function I get an error:

library(tcltk)

tt <- tktoplevel()
lb <- tklabel(tt, text = "Read this")
tkpack(lb)
tkget(lb, "text") # TODO Does not work

> Error in structure(.External(.C_dotTclObjv, objv), class = "tclObj") : 
  [tcl] bad option "get": must be cget or configure.

How do I call tkget correctly (there is no proper documentation available)? Or how could I read the current value of the text property another way?

The solution is:

library(tcltk)

tt <- tktoplevel()
lb <- tklabel(tt, text = "Read this", padx=10)
tkpack(lb)
tclvalue(tkcget(lb, "-text"))
tclvalue(tkcget(lb, "-padx"))
tkdestroy(tt)

Lessons learned:

  1. Don't mix up tkget with tkcget (the former is the get command of a text widget, the latter is a generic command to read widget properties.

  2. The widget property that you want to read must be specified as string with a hyphen (this is different from the way you specify the same property during creation of the widget in R using tcltk - see the padx example in the code above).

  3. As usual: You have to convert the result of tkcget from a TCL value into an R value using the tclvalue function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM