mhearse has asked for the wisdom of the Perl Monks concerning the following question:

I have a Tk gui which interfaces with a database. Right now, I just hard code the username/password into the script. I'd like to prompt for it using a login screen. I'd like the login screen to appear when the script is initially run. Then the main app would be launched if login is successful. I'm not sure how to incorporate this into my main program. I believe the login window is going to have to be a seperate mainwindow. I don't think a dialogbox will do. Any suggestions? Thanks much.

I kind of like that way it is handled here

Replies are listed 'Best First'.
Re: Suggestions for Tk login screen
by Popcorn Dave (Abbot) on May 01, 2005 at 07:17 UTC
    Can't you just open it as a child window initially or minimize your main window and open a new window and pass the information back? Or even just start with something like the graphic you linked to, pass the information and destroy the window, and move on.

    Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.
Re: Suggestions for Tk login screen
by zentara (Cardinal) on May 01, 2005 at 12:09 UTC
    There are a couple of ways to do it, like you could make a "custom toplevel for login" and withdraw it, then deiconify it when you want to login. But you can use a Dialog, and a sample script is below. I just whipped this up to show you the idea, but it can be improved.
    #!/usr/bin/perl use strict; use Tk; use Tk::DialogBox; my $running = 0; my $mw = new MainWindow(-title=>'My Main App'); my $text = $mw->Text(); my $loginbut = $mw->Button(-text=>'Login', -command => \&login)->pack; my $logoffbut = $mw->Button(-text=>'LogOff', -command => sub {$running = 0} )->pack; &login; MainLoop; ############################################################### sub login{ return if $running; my $dialog = $mw->DialogBox( -title => "Login", -buttons => [ "OK", "Cancel" ] ); my $server = 'default'; my $servl = $dialog->add( "Label", -text => 'Server' )->pack(); my $servd = $dialog->add( "Entry", -textvariable => \$server )->pack(); my $name = 'default user'; my $namel = $dialog->add( "Label", -text => ' Name ' )->pack(); my $named = $dialog->add( "Entry", -textvariable => \$name )->pack(); my $passwd; my $passl = $dialog->add( "Label", -text => 'Password' )->pack(); my $passd = $dialog->add( "Entry", -show => "*", -textvariable => \$passwd )->pack(); my $retlog = 0; if ( $dialog->Show() eq "OK" ) { print "$server\t$name\t$passwd\n"; #this line is only for demo #do your stuff here, like reprompt if the #password is bad, or whatever you want # here you would make a callback to a real login sub, # and if it is good, then enable the main gui $retlog = &real_login($server,$name,$passwd); } if($retlog == 1){ &start_main } else { &login} } #########################################################3 sub real_login{ my($server,$name,$passwd) = @_; my $login_stat = 0; #do your stuff here... eally tring to connect to server #assume good if( $passwd == 1 ){ $login_stat = 1; } return $login_stat; } ########################################################### sub start_main{ return if $running; $text->pack; $running = 1; }

    I'm not really a human, but I play one on earth. flash japh
      Question: can I use this if the bulk of my application is non-Tk? In other words, say I'd like to present a dialog to the user when my app starts, but once they complete the dialog I'd like to go off and do something else that doesn't involve a GUI. I notice that both Tk::Dialog and Tk::DialogBox require a parent widget. Is there a way I could use one of these as my main window, something like this:
      my $dlg = Tk::DialogBox->new(-title => 'Login', -buttons => ['Ok', 'Ca +ncel']); # this line doesn't work $dlg->add('Label', -text => 'User name')->pack; my $user_entry = $dlg->add('Entry')->pack; $dlg->add('Label', -text => 'Password')->pack; my $pwd_entry = $dlg->add('Entry', -show => '*')->pack; my $btn = $dlg->Show; my $username = $user_entry->get; my $pwd = $pwd_entry->get; if ($btn eq 'Ok') { print "logging in with user $username and password $pwd\n"; }
      and skip the MainLoop?
        Yes, you can make a Tk popup for information, then have it finish and let the rest of the program run without a gui. When the Tk EventLoop is destroyed, the main program will pickup execution right after the MainLoop line. Usually, nothing is there, and it exists, but check out this example.
        #!/usr/bin/perl use strict; use Tk; use Tk::DialogBox; my $logincount = 0; my $mw = new MainWindow(); $mw->withdraw; #hide main window &login; MainLoop; #rest of your script for(1..100){print "$_\n"; sleep 1} ############################################################### sub login{ my $dialog = $mw->DialogBox( -title => "Login", -buttons => [ "OK", "Cancel" ] ); my $server = 'default'; my $servl = $dialog->add( "Label", -text => 'Server' )->pack(); my $servd = $dialog->add( "Entry", -textvariable => \$server )->pack(); my $name = 'default user'; my $namel = $dialog->add( "Label", -text => ' Name ' )->pack(); my $named = $dialog->add( "Entry", -textvariable => \$name )->pack(); my $passwd; my $passl = $dialog->add( "Label", -text => 'Password' )->pack(); my $passd = $dialog->add( "Entry", -show => "*", -textvariable => \$passwd )->pack(); my $retlog = 0; if ( $dialog->Show() eq "OK" ) { print "$server\t$name\t$passwd\n"; #this line is only for demo #do your stuff here, like reprompt if the #password is bad, or whatever you want # here you would make a callback to a real login sub, # and if it is good, then enable the main gui $retlog = &real_login($server,$name,$passwd); } if($retlog == 1){ $mw->destroy } else { $logincount++; if( $logincount > 3 ) {exit} &login; } } #########################################################3 sub real_login{ my($server,$name,$passwd) = @_; my $login_stat = 0; #do your stuff here... eally tring to connect to server #assume good if( $passwd == 1 ){ $login_stat = 1; } return $login_stat; } ###########################################################

        I'm not really a human, but I play one on earth. flash japh
      Thanks for the reply. This solution works well with my current program. Now I'm going to investigate secure authentication via SSL or SASL.