Home > Archive > PerlTk > February 2006 > Need help learning to write modules
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Need help learning to write modules
|
|
|
| Hi Folks,
I am trying to learn how to write modules and have a simple case and need
help with it.
I have 1 'entry' and one 'checkbutton' widgets. What I want is to:
1- Selecting the checkbutton to *enable* the entry and clearing the
checkbutton
to *disable* the entry.
2- does any one have a very simple example of how to use *Callbacks*
Thanks a lot
-sm
here is my widget:
========================================
=
use Tk::widgets qw/Checkbutton Entry /;
use base qw/Tk::Frame/;
use strict;
Construct Tk::Widget 'ChkEntry';
sub ClassInit {
my( $class, $mw ) = @_;
$class->SUPER::ClassInit( $mw );
}
sub Populate {
my( $self, $args ) = @_;
$self->SUPER::Populate($args);
my $e = $self->Entry()->pack( -side => 'right');
my $c = $self->Checkbutton( -offvalue => 'disabled',
-onvalue => 'normal',
)->pack( -side => 'left');
$self->Advertise('checkbutton' => $c);
$self->Advertise('entry' => $e);
$self->ConfigSpecs
(
## Basics
-font => [ 'ADVERTISED', 'font', 'Font', undef ],
-text => [ $c, 'text', 'Text', undef ],
-justify => [ $c, 'justify', 'Justify', 'left' ],
-foreground => [ $c, 'foreground', 'Foreground', undef ],
-background => [ $c, 'background', 'Background', undef ],
-height => [ $c, 'height', 'height', undef ],
-variable => [ $c, 'variable', 'Variable', undef ],
-width => [ $e, 'width', 'Width', 8],
-textvariable => [ $e, 'textvariable', 'Textvariable', undef ],
-state => [ $e, 'state', 'State', undef ],
## both widgets
-state => [ [$e,$c], 'state', 'State', 'normal' ],
);
}
=============================
here is my test script for it
use Tk;
require ChkEntry;
my $cvar = 1;
my $evar = '';
my $top = MainWindow->new( -title => 'ChkEntry Test');
$top->resizable(1,0);
my $ce = $top->ChkEntry(-text => 'gahsdghasgdhasgd',
-variable => \$cvar,
-textvariable => \$evar,
#-state => 'disabled'
)->pack();
my $b = $top->Button( -text => 'Print',
-command => sub
{
print"=>$cvar, $evar<==\n";
}
)->pack( -side => 'bottom',
-padx => 3
);
MainLoop;
#:-)
| |
| zentara 2006-02-01, 7:02 pm |
| On Wed, 01 Feb 2006 06:48:02 GMT, "sm" <imfeaw5672@pacbell.net> wrote:
>Hi Folks,
>
>I am trying to learn how to write modules and have a simple case and need
>help with it.
>
>I have 1 'entry' and one 'checkbutton' widgets. What I want is to:
>
> 1- Selecting the checkbutton to *enable* the entry and clearing the
>checkbutton
> to *disable* the entry.
>
> 2- does any one have a very simple example of how to use *Callbacks*
>
>Thanks a lot
>-sm
I made a couple of changes to your code. First I made everything a part
of $self, so instead of $cb, you have $self->{'cb'}, etc. That way, you
can just pass $self to all your subs, and can access everything without
needing globals.
Second, I changed your on and off states from text strings to 0 and 1,
to make the logic easier.
#!/usr/bin/perl
use warnings;
use strict;
package ChkEntry;
use base qw/Tk::Frame/;
use strict;
Construct Tk::Widget 'ChkEntry';
sub ClassInit {
my( $class, $mw ) = @_;
$class->SUPER::ClassInit( $mw );
}
sub Populate {
my( $self, $args ) = @_;
$self->SUPER::Populate($args);
$self->{'entry'} = $self->Entry()->pack( -side => 'right');
$self->{'cb'} = $self->Checkbutton(
-offvalue => 0,
-onvalue => 1,
-command=> sub{ SetState($self) } )->pack( -side => 'left');
$self->Advertise('checkbutton' => $self->{'cb'} );
$self->Advertise('entry' => $self->{'entry'} );
$self->ConfigSpecs
(
## Basics
-font => [ 'ADVERTISED', 'font', 'Font', undef ],
-text => [ $self->{'cb'}, 'text', 'Text', undef ],
-justify => [ $self->{'cb'}, 'justify', 'Justify', 'left' ],
-foreground => [ $self->{'cb'}, 'foreground', 'Foreground',
undef ],
-background => [ $self->{'cb'}, 'background', 'Background',
undef ],
-height => [ $self->{'cb'}, 'height', 'height', undef ],
-variable => [ $self->{'cb'}, 'variable', 'Variable', undef ],
-width => [ $self->{'entry'}, 'width', 'Width', 8],
-textvariable => [ $self->{'entry'}, 'textvariable',
'Textvariable', undef ],
-state => [ $self->{'entry'}, 'state', 'State', undef ],
## both widgets
-state => [ [$self->{'entry'},$self->{'cb'}], 'state',
'State', 'normal' ],
);
}
sub SetState {
my $self = shift;
my $state = ${ $self->cget(-variable) };
#print "checkbutton->", $state,"\n";
$self->{'entry'}->configure( -state => $state ? 'normal' :
'disabled' );
}
1;
########################################
###############
package main;
use Tk;
#require ChkEntry;
my $cvar = 1;
my $evar = '';
my $top = MainWindow->new( -title => 'ChkEntry Test');
$top->resizable(1,0);
my $ce = $top->ChkEntry(-text => 'gahsdghasgdhasgd',
-variable => \$cvar,
-textvariable => \$evar,
#-state => 'disabled'
)->pack();
my $b = $top->Button( -text => 'Print',
-command => sub{
print"=>$cvar, $evar<==\n";
})->pack( -side => 'bottom', -padx => 3 );
MainLoop;
__END__
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
|
|
|
|
|