Version 0 of A little login dialog

Updated 2002-03-13 16:20:07

Richard Suchenwirth - Here is a little proc that brings up a username/password widget, the password field being made unreadable as is usual, and grabs focus until terminated with "OK", "Cancel" or <Return>. It returns a list of two elements: username and password, or an empty list if canceled.

Much can be done to improve this - just take it as a first shot!

 proc tk_login {w {title "Login please"}} {
    toplevel $w -borderwidth 10
    wm title $w $title
    label  $w.u -text "User name:"
    entry  $w.user -textvar _username
    label  $w.p -text "Password:"
    entry  $w.pass -show * -textvar _password
    label  $w.dummy -text ""
    button $w.ok -text OK -command {set _res [list $_username $_password]}
    button $w.cancel -text Cancel -command {set _res {}}
    grid   $w.u $w.user -         -sticky wns
    grid   $w.p $w.pass -         -sticky wns
    grid   $w.dummy x   x
    grid   x    $w.ok   $w.cancel -sticky news
    bind $w <Return> [list $w.ok invoke]
    raise $w
    grab set $w
    vwait _res
    destroy $w
    return $::_res
 }

# Silly little test...

 pack [text .t]
 update
 set password ""
 while {$password != "secret"} {
    set userpass [tk_login .login]
    set password [lindex $userpass 1]
 }
 .t insert end "[lindex $userpass 0] logged in\n"

Tk - Arts and crafts of Tcl-Tk programming