5

I have a long-ish script to do some data analysis, and it has to connect to several databases. One of the databases tends to update my password more frequently than I like, so I'd like to add a popup box for me to enter my current password. I found this example but don't understand enough tcltk to see hwo to return my value when the dialog box is dismissed. I've thought of defining it as a global variable (<<-) within the OnOK function, but this seems messy

require(tcltk)
tt<-tktoplevel()
Name <- tclVar("Password")
entry.Name <-tkentry(tt,width="20",textvariable=Name)
tkgrid(tklabel(tt,text="Enter Password"))
tkgrid(entry.Name)
OnOK <- function()
{
    NameVal <- tclvalue(Name)
    tkdestroy(tt)
}
OK.but <-tkbutton(tt,text="   OK   ",command=OnOK)
tkbind(entry.Name, "<Return>",OnOK)
tkgrid(OK.but)
tkfocus(tt)
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
PaulHurleyuk
  • 8,009
  • 15
  • 54
  • 78

3 Answers3

4

You answered your own question: you do in fact need <<- in these tcltk examples---see the various posts by Peter over the years, or other examples floating around, other CRAN packages using, or even in the package itself. Try

  library(tcltk)
  demo("tkcanvas")

and see how it treats e.g. lastX, lastY.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
4

You might find the ginput function of gWidgets wraps up what Greg Snow suggests for you:

require(gWidgets)
options(guiToolkit="tcltk") 
NameVal <- ginput("Enter your passsword:")

NameVal will contain the entered string or NA if you cancel.

jverzani
  • 5,600
  • 2
  • 21
  • 17
1

You can use the tkwait.window or tkwait.variable functions. Use the above code to pop-up the window, then use one of the wait functions (tkwait.window will stop the script from proceeding until the window is destroyed, essentially when you click on OK). Then when the script proceeds you will have your value in the variable within the script/function and can just use it there without needing to assign a global variable.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110