简体   繁体   English

从 R 中的 TclTk GUI 返回变量

[英]Returning variable from TclTk GUI in R

I've followed the TclTk example for creating a listbox to let the user choose their favorite fruit (see code below).我已经按照 TclTk 示例创建了一个列表框,让用户选择他们最喜欢的水果(参见下面的代码)。 The example prints the user's choice and that's it.该示例打印用户的选择,就是这样。 However, I want to use that choice later in my script to do various things (use it in plot titles, filenames, etc.) How do I return the user's choice from a TclTk GUI to the rest of my script?但是,我想稍后在我的脚本中使用该选择来做各种事情(在情节标题、文件名等中使用它)我如何将用户的选择从 TclTk GUI 返回到我的脚本的其余部分?

library(tcltk2)

win1 <- tktoplevel()
win1$env$lst <- tk2listbox(win1, height = 4, selectmode = "single")
tkgrid(tk2label(win1, text = "What's your favorite fruit?", justify = "left"),
padx = 10, pady =c(15, 5), sticky = "w")
tkgrid(win1$env$lst, padx = 10, pady = c(5, 10))
fruits <- c("Apple", "Orange", "Banana", "Pear", "Apricot")
for (fruit in fruits)
  tkinsert(win1$env$lst, "end", fruit)
# Default fruit is Banana.  Indexing starts at zero.
tkselection.set(win1$env$lst, 2)

onOK <- function() {
  fruitChoice <- fruits[as.numeric(tkcurselection(win1$env$lst)) + 1]
  tkdestroy(win1)
  msg <- paste0("Good choice! ", fruitChoice, "s are delicious!")
  tkmessageBox(message = msg)
}
win1$env$butOK <-tk2button(win1, text = "OK", width = -6, command = onOK)
tkgrid(win1$env$butOK, padx = 10, pady = c(5, 15))

How do I return the user's choice from a TclTk GUI to the rest of my script?如何将用户的选择从 TclTk GUI 返回到脚本的其余部分?

This depends on what you want to do这取决于你想做什么

Option 1:选项1:

If you don't need the GUI anymore you destroy it after extracting the selected settings from the GUI in R variables.如果您不再需要 GUI,您可以在从 R 变量中的 GUI 中提取选定的设置后销毁它。 This is exactly what you do in the onOK "event handler" function.这正是您在onOK “事件处理程序”函数中所做的。

You should add the following R command to wait for the tcltk window to be closed before continuing the execution of the R code after this line:您应该添加以下 R 命令以等待tcltk窗口关闭,然后再继续执行此行之后的 R 代码:

# Wait for the window to be closed
tkwait.window(win1)

You can then use the values in R variables (filled from the GUI settings in the event handler function) in your business logic eg by adding the business logic code at at the end of your code:然后,您可以在业务逻辑中使用 R 变量中的值(从事件处理程序函数中的 GUI 设置填充),例如通过在代码末尾添加业务逻辑代码:

choice <- tkmessageBox(message=paste("Do you want to buy", fruitChoice, "now?"), type = "yesno", icon="question")

Option 2:选项 2:

Your business logic is triggered (executed) in the event handler functions (without destroying/closing the window) to visualize the output ("state change") of the business logic.您的业​​务逻辑在事件处理函数中被触发(执行)(不破坏/关闭窗口)以可视化业务逻辑的输出(“状态更改”)。

In this GUI driven paradigm you add the business logic code into the event handler functions directly without calling tkdestroy .在这个 GUI 驱动的范例中,您可以直接将业务逻辑代码添加到事件处理函数中,而无需调用tkdestroy

For closing the windows you add "exit", "close", "cancel"... buttons.要关闭窗口,您可以添加“退出”、“关闭”、“取消”...按钮。 In the event handler function of these buttons you call tkdestroy .在这些按钮的事件处理函数中,您调用tkdestroy

There are command parameters available with listboxes (need to know which package you used for creating it).列表框有可用的命令参数(需要知道您使用哪个包来创建它)。 eg there is a parameter "-selectioncommand".例如,有一个参数“-selectioncommand”。 You can set a namespace variable to access the selected value.您可以设置命名空间变量来访问所选值。

eg -selectioncommand {set ::xyz::test} this should be used while creating a widget.例如 -selectioncommand {set ::xyz::test} 这应该在创建小部件时使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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