Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How do I destroy a control after its creation in wxPerl?

by ZJ.Mike.2009 (Scribe)
on May 11, 2011 at 04:26 UTC ( #904074=perlquestion: print w/replies, xml ) Need Help??

ZJ.Mike.2009 has asked for the wisdom of the Perl Monks concerning the following question:

I've created a static text control inside the status bar control so that I can display colored text there like so:
my $status = Wx::StatusBar->new($parent,wxSB_RAISED); $status->SetForegroundColour(wxRED); my $warning = Wx::StaticText->new ($status, wxID_ANY,"Input Error!");
But how do I destroy the text control so that I can display status text in the same position without getting blocked? I've tried:
$warning->Destroy(); $status->SetStatusText("OK",0);
But it does not seem to have any effect. Any pointers? Thanks like always.

UPDATE

Thanks Anonymous Monks :)

I had tried the Hide and Show methods before posting the question but it didn't seem to work. But now after playing around with the demo code you kindly offered here, I think I am able to get the hide and show to work as expected. Thanks :) But I'm still missing something obvious. The following code shows my problem: why I can't destroy the control?

use strict; use warnings; use Wx ':everything'; my $window = Wx::Frame->new(undef, -1, "Colored Statusbar Text"); my $panel = Wx::Panel->new($window); my $button = Wx::Button->new( $panel, -1, "Toggle", ); Wx::Event::EVT_BUTTON( $button, -1, \&button_click, ); my $status = $window->CreateStatusBar(2, wxSB_RAISED); $window->Show; my $hit =1; Wx::SimpleApp->new->MainLoop; sub button_click { $hit++; my $warning = Wx::StaticText->new($status, -1, "Input Error",,[3,8 +]); $warning->SetForegroundColour(wxRED); if ($hit % 2) { $status->SetStatusText("", 0); $warning->Show; } else { $warning->Destroy; $status->SetStatusText("OK", 0); } }

UPDATE2

The hide and show methods seem to work:
use strict; use warnings; use Wx ':everything'; my $window = Wx::Frame->new(undef, -1, "Colored Statusbar Text"); my $panel = Wx::Panel->new($window); my $button = Wx::Button->new( $panel, -1, "Toggle", ); Wx::Event::EVT_BUTTON( $button, -1, \&button_click, ); my $status = $window->CreateStatusBar(2, wxSB_RAISED); my $warning = Wx::StaticText->new($status, -1, "Input Error",,[3,8]); $warning->SetForegroundColour(wxRED); $window->Show; my $hit =1; Wx::SimpleApp->new->MainLoop; sub button_click { $hit++; if ($hit % 2) { $status->SetStatusText("", 0); $warning->Show; } else { $warning->Hide; $status->SetStatusText("OK", 0); } }

Replies are listed 'Best First'.
Re: How do I destroy a control after its creation in wxPerl?
by Anonymous Monk on May 11, 2011 at 10:22 UTC
    You can simply ->Hide the warning box and set the built-in status text to anything.

    Then when you show a warning again, ->Show the warning box and clear the status text (or set the warning box to be exactly the size of the status field you're covering, with the help of Wx::Statusbar::GetFieldRect)

      Thanks Anonymous Monk, This helps.
Re: How do I destroy a control after its creation in wxPerl?
by Anonymous Monk on May 11, 2011 at 10:25 UTC
    like so ... But it does not seem to have any effect. Any pointers? Thanks like always.

    Well :) :D ;D if your code was runnable, you'd see that it does work, How do I post a question effectively?

    Proof of concept

    #!/usr/bin/perl -- use Wx; # First, define an application object class to encapsulate the applica +tion itself package HelloWorld; use base 'Wx::App'; # We must override OnInit to build the window sub OnInit { my $self = shift; my $frame = Wx::Frame->new(undef, # no parent window -1, # no window id 'Hello, World!', # Window title [-1, -1], # Position [-1, -1], # Size ); my $panel = Wx::Panel->new($frame); $frame->CreateStatusBar( 2, Wx::wxSB_RAISED() ); $frame->GetStatusBar->SetStatusText("--------------humpty",0); $frame->GetStatusBar->SetStatusText("dumpty",1); $frame->GetStatusBar()->SetForegroundColour(Wx::wxRED());; my $warning = Wx::StaticText->new ($frame->GetStatusBar(), -1,"Inp +ut Error!"); Wx::Event::EVT_BUTTON( $frame, Wx::Button->new($panel, -1, "DOIT"), sub { if( $warning ){ print "$_\n" for $frame->GetStatusBar()->GetChildren; +# only child is warning $warning->Destroy(); undef $warning; } $frame->GetStatusBar->SetStatusText("OK ". time ,0); }, ); $frame->Show(1); return 1; } # Create the application object, and pass control to it. package main; my $app = HelloWorld->new; $app->MainLoop; __END__
      A better test would be a simple one.
      #! /usr/bin/env perl use strict; use warnings; use Wx ':everything'; use Wx::Event ':everything'; my $window = Wx::Frame->new(undef, wxID_ANY, $0); my $panel = Wx::Panel->new($window); my $toggle = Wx::Button->new($panel, wxID_ANY, "&Toggle"); my $status = $window->CreateStatusBar(2, wxSB_RAISED); $status->SetStatusText("foo", 0); $status->SetStatusText("bar", 1); my $warning = Wx::StaticText->new($status, wxID_ANY, "warning"); $warning->SetBackgroundColour(Wx::Colour->new('yellow')); $warning->SetForegroundColour(Wx::Colour->new('red')); sub fit_warning { $warning->SetSize($status->GetFieldRect(0)) } EVT_SIZE($status, \&fit_warning); EVT_BUTTON($toggle, $toggle, sub { unless ($warning->IsShown) { $warning->SetLabel(scalar localtime); fit_warning(); $warning->Show; } else { $warning->Hide; } }); $warning->Hide; $window->Show; Wx::SimpleApp->new->MainLoop;
        A better test would be a simple one.

        Hiding makes sense, but I don't presume to know what ZJ is really after :)

      Thanks Anonymous Monk, I've updated my post with code that demonstrates my problem. I seem to unable to create a control in a subroutine and then immediately destroy it if a condition is true.
        I seem to unable to create a control in a subroutine and then immediately destroy it if a condition is true.

        I assume this is a moot point by now, since the show/hide approach is best, but here it is, for posterity :)

        Your code does not attempt to do that

        Ignoring that for a minute, this is common for all GUI frameworks, when MainLoop is running, calling Destroy from a callback doesn't actually destroy a window immediately, it schedules it for destruction. See

        Now back to the code, this will show you the error
        sub button_click { $hit++; warn "hit $hit "; my $warning = Wx::StaticText->new($status, -1, "Input Error $hit " +,,[3,8]); $warning->SetForegroundColour(wxRED); if ($hit % 2) { $status->SetStatusText("", 0); $warning->Show; } else { $warning->Destroy; $status->SetStatusText("OK", 0); } warn "[\n", map({ "\t$_,\n" } $status->GetChildren ), "\] "; }
        The output is
        $ perl wx.904074.event.pl hit 2 at wx.904074.event.pl line 34. [ ] at wx.904074.event.pl line 46. hit 3 at wx.904074.event.pl line 34. [ Wx::StaticText=HASH(0xed4c34), ] at wx.904074.event.pl line 46. hit 4 at wx.904074.event.pl line 34. [ Wx::StaticText=HASH(0xed4c34), ] at wx.904074.event.pl line 46. hit 5 at wx.904074.event.pl line 34. [ Wx::StaticText=HASH(0xed4c34), Wx::StaticText=HASH(0xed4be4), ] at wx.904074.event.pl line 46. hit 6 at wx.904074.event.pl line 34. [ Wx::StaticText=HASH(0xed4c34), Wx::StaticText=HASH(0xed4be4), ] at wx.904074.event.pl line 46. hit 7 at wx.904074.event.pl line 34. [ Wx::StaticText=HASH(0xed4c34), Wx::StaticText=HASH(0xed4be4), Wx::StaticText=HASH(0xed4c44), ] at wx.904074.event.pl line 46. hit 8 at wx.904074.event.pl line 34.
        With each iteration you're creating a new statictext, but you only Destroy it every other iteration, so the list of statictext objects keeps growing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://904074]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2023-01-29 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?