Patrik
Patrik

Reputation: 341

Wxperl create widget but don't place it

How can I create a widget without it being placed on its parent?

Here's a minimal example.

package MyApp;
use strict;
use warnings;
use Wx;
use base 'Wx::App';

sub OnInit {
    my ($self) = @_;
    my $frame
        = Wx::Frame->new( undef, -1, 'Test', [ -1, -1 ], [ 250, 150 ], );
    my $sizer = Wx::GridBagSizer->new( 0, 0 );

    my $btn1 = Wx::Button->new( $frame, -1, '1' );
    my $btn2 = Wx::Button->new( $frame, -1, '2' );

    $sizer->Add( $btn1, Wx::GBPosition->new( 2, 2 ) );
    $frame->SetSizer($sizer);
    $frame->Show(1);
    1;
}

package main;
MyApp->new->MainLoop;

This yields

enter image description here

I want only what is placed in the sizer (button 1) to show.

Upvotes: 2

Views: 76

Answers (2)

brian d foy
brian d foy

Reputation: 132812

You can hide things by calling $thing->Show(0). I added:

 $btn2->Show(0);

The layout is still kinda funny because the space for the widget is still there—it's just not visible. So, it's still "placed". Maybe you want to create the control somewhere else that you can size on its own.

You have to hide the widget before the call to Layout.

See Hiding Controls Using Sizers

Upvotes: 2

VZ.
VZ.

Reputation: 22688

All non top-level windows are created shown by default. If you don't want them to appear on the screen, you need to hide them. The best way to do it is to hide them before actually creating the real window, which can be achieved in C++ by creating the window without giving it any parameters and then calling Create() with the same parameters you would normally use when creating it.

I'm not sure if this is exposed in wxPerl. If it is, something like this

    my $btn2 = Wx::Button->new();
    $btn2->Hide(); # or $btn2->Show(false)
    $btn2->Create($frame, -1, '2' );

should work;

If not, you can still hide it and if you do it before showing the frame, it still won't be visible for the user.

Upvotes: 2

Related Questions