From: Alexander F. G. <per...@bl...> - 2001-10-03 16:04:49
|
The recent wxPerl articles on www.perl.com left me curious, so I decided = to have a closer look. The result is the code below a simple Login dialog (two = text entry fields, two text labels & two buttons) that I cobbled together = from samples in the mailinglist and sourceforge's samples archive..=20 Writing the code proved fairly easy, despite a nagging feeling that I = don't realy understand what's going on... but I ended up with two problems I = can't seem to solve: 1.I can't seem to find a sample for a (blind) textentry,=20 that doesn't show the text typed, but displays '*' for=20 each character typed. 2. Also, changing $main::DEBUG to 1 results in an error message=20 "Use of uninitialized value in subroutine entry at D:\Projekte\Learning\Perl\wx\wxLogin.pl line 141."=20 that I don't understand. The error occurs on the "$app->MainLoop();" line, btw. As you might suspect, I'm using ActivePerl under Windows 2000.=20 wxPerl seems to be version 0.07 -------------- #!/usr/local/bin/perl -w # Pragmas use strict; # Modules use Wx; #########################################################################= ###### # Declare my own Frame-Class based on Wx::Frame #########################################################################= ###### package MyFrame; use base qw(Wx::Frame); #------------------------------------------------------------------------= ------ use Wx::Event qw/EVT_BUTTON /; #------------------------------------------------------------------------= ------ # Build the dialog sub new { my $class =3D shift; my $self =3D $class->SUPER::new(@_); = #------------------------------------------------------------------------= -- # create panel for the dialog $self->{-panel} =3D Wx::Panel->new( $self, -1); = #------------------------------------------------------------------------= -- # two text entry fields with Labels in front of them $self->addTextCtrl( 'UserID' , [10,12],[80, 10], [160, -1] ); $self->addTextCtrl( 'Password', [10,32],[80, 30], [160, -1] ); = #------------------------------------------------------------------------= -- # two Buttons for OK and Cancel $self->addButton("OK" ,[10 ,60 ],\&OkButton); $self->addButton("Cancel",[160,60 ],\&CancelButton); return $self; } #------------------------------------------------------------------------= ------ # increment and return the ID counter for dialog elements sub nextID { my $self=3D shift; $self->{-ID}=3D0 unless defined $self->{-ID}; $self->{-ID} =3D $self->{-ID}+1; return $self->{-ID}; } #------------------------------------------------------------------------= ------ # return the current ID counter for dialog elements sub getID { my $self=3D shift; $self->{-ID}=3D0 unless defined $self->{-ID}; return $self->{-ID}; } #------------------------------------------------------------------------= ------ # Add a Button to the Dialog sub addButton { my ($self,$label,$coord,$action) =3D @_; Wx::Button->new( $self->{-panel} # Parent window ,$self->nextID() # Control id ,$label # Label ,$coord # position X, Y ); EVT_BUTTON($self, $self->getID, $action) if $action; return $self; } #------------------------------------------------------------------------= ------ # Add a text label followed by a text entry to the dialog sub addTextCtrl { my ( $self ,$label ,$coordLabel ,$coordAnchor ,$coordSize ,$default ) =3D @_; = #------------------------------------------------------------------------= --- # avoid undefined default values $default=3D'' unless defined $default; = #------------------------------------------------------------------------= --- # Add the Label Wx::StaticText->new( $self->{-panel} # Parent window ,$self->nextID() # Control id ,$label # Label ,$coordLabel # position X, Y ); = #------------------------------------------------------------------------= --- # Add the text control $self->{-textvar} ->{$label} =3D Wx::TextCtrl->new( $self->{-panel} # Parent = window ,$self->nextID() # Control id ,$default # default entry ,$coordAnchor # position X, Y ,$coordSize # size X, Y ); return $self; } #------------------------------------------------------------------------= ------ # handle the OK-Button event sub OkButton { my($self, $event) =3D @_; print "Button: OK\n" if ( $main::DEBUG);; = #------------------------------------------------------------------------= --- # execute the action routine in main if it is defined # pass along the login parameters.. main::Action( $self->{-textvar}->{'UserID'}->GetValue ,$self->{-textvar}->{'Password'}->GetValue ) if defined &main::Action; = #------------------------------------------------------------------------= --- # close the dialog $self->Destroy(); } #------------------------------------------------------------------------= ------ # handle the CancelButton event sub CancelButton { my($self, $event) =3D @_; print "Button: CANCEL\n" if ( $main::DEBUG);; = #------------------------------------------------------------------------= --- # close the dialog $self->Destroy(); } #########################################################################= ###### # Declare my own Application class derived from Wx::App #########################################################################= ###### package wxLogin; use base qw(Wx::App); #------------------------------------------------------------------------= ------ sub OnInit{ my $self =3D shift; my $frame =3D MyFrame->new( undef # Parent window ,-1 # Window id ,'wxLogin example' # Title ,[10,10] # position X, Y ,[250, 120] # size X, Y ); $self->SetTopWindow($frame); # Define the toplevel window $frame->Show(1); # Show the frame 1; } #------------------------------------------------------------------------= ------ sub OnExit { Wx::LogMessage( "Exiting wxLOGIN" ) if ( $main::DEBUG); } #########################################################################= ###### package main; my( $app ) =3D wxLogin->new(); $main::DEBUG=3D0; $app->MainLoop(); #------------------------------------------------------------------------= ------ # this is the stub action, should do the DB-login and Data output = routines...=20 # later .. sub Action { my($uid,$pwd)=3D@_;print "Action($uid,$pwd)!\n"; } |