Re: Creating a derived widget that is a -container

Lidie Steve <[email protected]>
Newsgroups gmane.comp.lang.perl.tk
Message-ID <[email protected]>
On Dec 18, 2004, at 8:53 PM, H. Wade Minter wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I posted this to the newsgroup a couple of weeks ago and got no 
> response, so I'm posting to the mailing list in hopes of getting a 
> look from people who read this and not the group.
>
> I'm creating a widget that is based on Tk::Frame. I want this widget 
> to be a container so that another toplevel can -use it for embedding.
>
> The trick is that I want the -container flag to get set on this widget 
> DURING its construction, not afterward. I'm pretty confused as to how 
> to do this.  I'm using Chapter 14 in Mastering Perl/Tk as a guide for 
> creating the Tk::Frame-based widget.

If your widget ISA  Tk::Frame, then -container is already defined for 
you, e.g this works for me:

#!/usr/local/bin/perl -w
use Tk::MyFrame;
use Tk;
use strict;

my $mw = MainWindow->new;
$mw->title( 'My Container Frame' );
my $mf = $mw->MyFrame(qw/ -container 1 /)->pack;
my $winid = $mf->id;

my $mw2 = MainWindow->new( -use => hex( $winid ) );
my $l = $mw2->Label(qw/ -text HelloWorld /)->pack;

MainLoop;
  .....

$Tk::MyFrame::VERSION = '1.0';

package Tk::MyFrame;

use base qw/Tk::Frame/;
use strict;

Construct Tk::Widget 'MyFrame';

sub ClassInit {

     my($class, $mw) = @_;

     $class->SUPER::ClassInit($mw);

} # end ClassInit

sub CreateArgs {

     my($class, $mw, $args) = @_;

     my(%args) = ();
     ($class->SUPER::CreateArgs($mw, $args), %args);

} # end CreateArgs

sub SetBindtags {

     my($self) = @_;

     $self->SUPER::SetBindtags;

} # end SetBindtags

sub Populate {

     my($self, $args) = @_;

     $self->SUPER::Populate($args);

} # end Populate

1;


>
> Should this be done during the ClassInit sub, or the Populate sub?

Options valid only at widget creation time are defined in CreateArgs();

>
> I'm trying it with Populate, but can't get it to work. I've got:
>
> sub Populate {
>   my ($self, $args) = @_;
>   $self->SUPER::Populate($args);
>   $self->configure(-container=>1);
> ...
>
> }
>
> However, during the Populate, if I later try to -use $self's widget 
> id, I get:
>
> Tk::Error: Can't set -container to `1' for 
> Tk::MyWidget=HASH(0x8b3f00): can't modify -container option after 
> widget is created at 
> /Library/Perl/5.8.1/darwin-thread-multi-2level/Tk/Configure.pm line 
> 46.
>
> If I try to do something like:
>
> sub Populate {
>   my ($self, $args) = @_;
>   $args->{-configure} = 1;
>   $self->SUPER::Populate($args);
>   $self->configure(-container=>1);
> ...
>
> }
>
> Then it just seems to silently fail like:
>
> window ".foo" doesn't have -container option set at 
> /Library/Perl/5.8.1/darwin-thread-multi-2level/Tk/Widget.pm line 190.
>
> Any suggestions as to what magic I need to do to enable this will be 
> welcomed.

Pages 320 - 321 of MPTK go into details on widget instantiation:

Tk::Widget::new, the Real Perl/Tk Widget Constructor
	In chronological order, Tk::Widget::new  performs these six major 
steps when creating a Perl/Tk widget:

	1. 	Calls ClassInit  to perform class initialization, such as creating 
class bindings, once, as the first widget of the class is created.
	2. 	Calls CreateArgs  to perform argument processing that is only 
applicable at widget creation time (as opposed to later configuration). 
A Toplevel's -- colormap  option is an example.
	3. 	Actual widget creation and blessing into the proper package. The 
only  arguments specified at this time are the keyword/value pairs 
returned by CreateArgs .
	4. 	Calls SetBindtags  to initialize the widget's bindtags list. See  
Chapter 15 for details.
	5. 	Calls Populate  to perform widget initialization. Populate  calls 
ConfigSpecs  to generate configuration specifications and Delegates  to 
describe how methods are dispatched to subwidgets. Populate is called 
only because Tk::Derived in the widget's @ISA  hierarchy.
	6. 	Actually configures the widget using the configuration 
specification hash generated by the previous call to Populate .

	As mega-widget writers, we have access to the widget in steps 1, 2, 4 
and 5, detailed in the following sections.

>
> Thanks,
> Wade
>

Steve
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.