如何在 R 中访问和修改 Tcl/Tk 对象的同级对象 [英] How to access and modify a sibling of a Tcl/Tk object in R
问题描述
简而言之:
我在 R 中使用 tcltk
包.但非 R 用户也可能会提出想法并提供 R 以外的其他语言的示例.我在 R
并且想分别访问和修改每个对象.我该怎么做?
I use tcltk
package in R. But non-R users may suggest ideas too and provide examples in other language than R.
I have a list of Tcl/Tk objects in R <Tcl> .1.1.1.1 .1.1.1.2 .1.1.1.3
and want to access and modify each object separately. How can I do it?
或者如果我有按钮 A 对象,我如何访问和修改按钮 B 对象?
Or If I have button A object, how can I access and modify button B object?
详情:
我有以下小部件:
创建它的代码:
library(tcltk)
top <- tktoplevel()
frame_1 <- tkframe(top)
tkgrid(frame_1)
frame_n <- tkframe(frame_1)
tkgrid(frame_n)
b1 <- ttkbutton(frame_n, text = "button A")
b2 <- ttkbutton(frame_n, text = "button B")
b3 <- ttkbutton(frame_n, text = "button c")
tkgrid(b1, b2, b3)
假设我只能访问 b1
:
class(b1)
# [1] "tkwin"
我想访问和修改 b1
的兄弟,就好像我有对象 b2
等(例如):
I want to access and modify the siblings of b1
as if I had objects b2
, etc. (for example):
tkcget(b2, "-text") # Get text
tkconfigure(b2, text = "New B") # Change text
通过使用 tkwinfo
,我设法访问了 b1
的父级并获得了兄弟姐妹列表(我不确定从技术上讲它是否是一个列表"),但我不知道,如何一一访问/修改它们:
By using tkwinfo
, I managed to access the parent of b1
and get a list of siblings (I'm not sure if technically it is a "list"), but I don't know, how to access/modify each of them one by one:
(parent_of_b1 <- tkwinfo("parent", b1))
# <Tcl> .1.1.1
(siblings_of_b1 <- tkwinfo("children", parent_of_b1))
# <Tcl> .1.1.1.1 .1.1.1.2 .1.1.1.3
class(siblings_of_b1)
# "tclObj"
我的尝试导致错误:
tkcget(siblings_of_b1, "-text")
# Error in structure(.External(.C_dotTclObjv, objv), class = "tclObj") :
# [tcl] invalid command name ".1.1.1.1 .1.1.1.2 .1.1.1.3".
很可能我不知道对对象进行子集化的方法.我该怎么做?
Most probably I don't know the way to subset the object. How can I do it?
更新:根据@Donal Fellows 的评论,我找到了解决方案.
UPDATE: based on the comments of @Donal Fellows, I found the solution.
函数 as.character()
完成这项工作.
Function as.character()
does the job.
(my_tcl_object <- tkwinfo("children", parent_of_b1))
# <Tcl> .1.1.1.1 .1.1.1.2 .1.1.1.3
as.character(my_tcl_object)
## [1] ".1.1.1.1" ".1.1.1.2" ".1.1.1.3"
在这种情况下,tclvalue()
+ strsplit()
同样有效:
In this situation, tclvalue()
+ strsplit()
works as well:
strsplit(tclvalue(my_tcl_object), " ", fixed = TRUE)[[1]]
## [1] ".1.1.1.1" ".1.1.1.2" ".1.1.1.3"
但是,一般来说(对于其他问题),as.character()
vs. tclvalue()
+ strsplit()
可能会给出不同的结果.
But, in general (for other problems), as.character()
vs. tclvalue()
+ strsplit()
may give different results.
推荐答案
问题在于 winfo children
子命令(使用底层 Tcl 名称)返回一个 Tcl 小部件标识符列表.一般来说,从其他语言处理这有点麻烦(因为处理引用规则的潜在问题)但因为生成的小部件标识符只包含 ASCII 数字和 .
字符和分隔符只是单个空格,只需按空格分割会给你正确的东西.
The issue is that the winfo children
subcommand (using the underlying Tcl name) returns a Tcl list of widget identifiers. In general, this is a bit messy to deal with from other languages (because of potential issues with handling quoting rules) but because the generated widget identifiers just contain ASCII digits and .
characters and the separators are just single spaces, simply splitting by space will give you the right thing.
(siblings_of_b1 <- strsplit(tkwinfo("children", parent_of_b1), " ", fixed = TRUE))
当然,您需要遍历结果列表.多个兄弟姐妹是多个兄弟姐妹.(另外,不要忘记这 包括 b1 本身;你没有要求实际的兄弟姐妹,而是父母的孩子.)
You'll need to iterate over the resulting list, of course. Multiple siblings are multiple siblings. (Also, don't forget that this includes b1 itself; you've not asked for the actual siblings, but rather the children of the parent.)
这篇关于如何在 R 中访问和修改 Tcl/Tk 对象的同级对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!