Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Clearing & Redrawing a Wx Window

by rcseege (Pilgrim)
on Sep 26, 2006 at 06:39 UTC ( [id://574864]=note: print w/replies, xml ) Need Help??


in reply to Clearing & Redrawing a Wx Window

Updated: changed references to unpack, unplace, ungrid to packForget, placeForget, and gridForget respectively. That was what I intended, but brain must have misfired. Also, corrected a typo in the code example.

I agree with Zentara that your application shouldn' t be trying to recreate itself, when it could recreate just the desired Panel. Better still, the application could create one instance of each screen and hide/show the appropriate screen on demand....

First, by way of comparison, in Perl/Tk this sort of thing can be accomplished a lot of different ways but it usually amounts to variations of the following:

  1. A Toplevel window (modal or otherwise) is launched from the main application to do some work. This Toplevel can either be created on-the-fly or created once and reused over and over again. (withdrawn and deiconified rather than destroyed then recreated) as Zentara suggested. Typically, this is faster and generally better on memory consumption.

  2. A window can be shown/hidden with a combination of pack/packForget, grid/gridForget, place/placeForget, etc. This is a lot like playing with the CSS visibility attribute for DHTML. You see this idea used for Wizards, Notebook-style apps, and other apps that use few Toplevels.

Of the two approaches, number 2 sounds closer to what you're trying to do. When using Wx, it's good to get familiar with the methods and Classes that have to do with Layout and display as quickly as possible.I recommend that you read up on the various Sizer implementations that provide Layout Management. These are slightly lower-level than what you might be used to using Tk, and closer to what's available in Java, Qt, and probably Gtk.

Some other critical methods to learn are Show(boolan), which will toggle visibility for a widget. Show is perhaps the closest equivalent to pack/packForget. Also, Layout, Update, and Refresh are useful.

Here's an example of one way to go about it -- there are plenty of other approaches. I think in your example, you were running afoul of a Layout issue.

use Wx; package MainFrame; use base qw(Wx::Frame); use Wx qw(wxVERTICAL) use Wx::Event qw( EVT_BUTTON ); sub new { my $class = shift; my $self = $class->SUPER::new(@_); my $sizer = Wx::BoxSizer->new(wxVERTICAL); my $panel = Wx::Panel->new($self, -1); $panel->SetSizer($sizer); ## Auth Button my $authBtn = Wx::Button->new($panel, -1, "Authorize"); EVT_BUTTON($self, $authBtn->GetId, \&AuthHandler); $sizer->Add($authBtn); $self->{AuthBtn} = $authBtn; ## Restart Button my $restartBtn = Wx::Button->new($panel, -1, "Restart"); EVT_BUTTON($self, $restartBtn->GetId, \&RestartHandler); $sizer->Add($restartBtn); $self->{RestartBtn} = $restartBtn; ## Initial Layout $restartBtn->Show(0); $sizer->SetSizeHints($self); $self->Layout; return $self; } sub AuthHandler { my $self = shift; $self->{AuthBtn}->Show(0); $self->{RestartBtn}->Show(1); } sub RestartHandler { my $self = shift; $self->{AuthBtn}->Show(1); $self->{RestartBtn}->Show(0); } ########################## ## Main Application Class ########################## package MyApp; use base qw(Wx::App); sub OnInit { my $self = shift; $frame = MainFrame->new(undef, -1, 'Test App'); $self->SetTopWindow($frame); $frame->Show(1); } ###################### ## Run App ###################### package main; use strict; use warnings; my $app = MyApp->new; $app->MainLoop;

Some general advice: use Sizers over absolute positioning, go ahead and let Wx generate the id by supplying -1 as the Id (so long as you keep a reference to the widget). It doesn't take much to expand that example and take it further. I think one avenue I would have pursued would be to create a Panel subclass for each screen, create each one once, and then only Show the one that was needed.

You might consider taking a look at: wxWizardPage (and wxWizard).

Rob

Replies are listed 'Best First'.
Re^2: Clearing & Redrawing a Wx Window
by spatterson (Pilgrim) on Sep 26, 2006 at 09:46 UTC
    Thanks, that works a charm.

    As I'd done some Tk development in the past, I was trying to replicate some of the placement tricks where you place (pack) the widgets within a top level frame which you can then clear with packForget & recall a sub to fill the frame again.


    just another cpan module author

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-18 00:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found