[R] tkentry that exits after RETURN?

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Aug 12 00:18:25 CEST 2008


2008/8/11 Ben Bolker <bolker at zoology.ufl.edu>:
>
>  I can set up an entry widget (thanks to an old
> post by Barry Rowlingson) that gets a password and
> exits when the user clicks on the "OK" button.

 Who? Oh dear, my past comes back to haunt me...

> getPassword <- function(){
>   require(tcltk)
>   tt <- tktoplevel()
>   pass=tclVar("")
>   label.widget <- tklabel(tt, text="Enter Password")
>   password.widget <- tkentry(tt,show="*",textvariable=pass)
>   ok <- tkbutton(tt,text="OK",default="active",
>                  command=function()tkdestroy(tt))
>   tkpack(label.widget, password.widget,ok)
>   tkwait.window(tt)
>   return(tclvalue(pass))
> }

 tkbind a function on the entry widget so that if <Return> is pressed
it destroys the window:

getPassword=function(){
  require(tcltk)
  tt <- tktoplevel()
  pass=tclVar("")
  label.widget <- tklabel(tt, text="Enter Password")
  password.widget <- tkentry(tt,show="*",textvariable=pass)
  tkbind(password.widget,"<Return>",function(){tkdestroy(tt)})
 ok <- tkbutton(tt,text="OK",default="active",
                 command=function()tkdestroy(tt))
  tkpack(label.widget, password.widget,ok)
  tkwait.window(tt)
  return(tclvalue(pass))
}

 How's that? See you in a few years...

Barry



More information about the R-help mailing list