TonyDonker has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am currently building a GUI in which I want to elaborate each menu item in a seperate module. This is a fragment of my main program:

use Tk 800; use Tk::Dialog; use event; use icit_messages; $top = new MainWindow; $top->title("As2Con Gui"); my $frame = $top->Frame(qw/-width 800 -height 300 -background white/)- +>pack; create_gui(); sub create_gui { $menubar = $top->Menu; $status_menu->command(-label => "~Events", -command => sub { even +t_function(\$frame) }); $status_menu->command(-label => "M~essages", -command => sub { m +essages_function(\$frame) }); ....
In module event.pm the function event_function is defined (a fragment):
sub event_function { my $top = shift; if ($view) { $view->packForget } $view = $$top->Frame(qw/-width 800 -height 400 -background whi +te/)->pack(-expand => 1, -fill => 'both'); ...
In module icit_messages.pm the function messages_function is defined (a fragment):
sub messages_function { my $top = shift; if ($view) { $view->packForget; } $view = $$top->Frame(qw/-width 400 -height 400 -background white/)-> +pack(); ...
The problem is when I select menu item Events everything goes OK in the current window, but when I select menu item Messages after I have selected Events the current window is enlarged and the output of Messages is below the output of Events in the same window. This problem does not occur when I select Messages or Events two times after each other...

What am I doing wrong?

Thanks,
Ton

Replies are listed 'Best First'.
Re: Menu Items & Modules...
by PodMaster (Abbot) on Feb 21, 2005 at 10:27 UTC
    Change event_function(\$frame) into event_function($frame) and avoid using $$top, since $top is already a reference.

    You should also Use strict warnings and diagnostics or die, and you should post a complete code snippet which demonstrates your problem (How (Not) To Ask A Question), as describing what happens with a mysterious module (Tk) is not nearly as useful as demonstrating what happens.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      OK I did: but how can I avoid that when clicking on the menu item the frame is constantly being printed out?
Re: Menu Items & Modules...
by castaway (Parson) on Feb 21, 2005 at 10:39 UTC
    It looks like you are expecting each of the sub modules to refer to the same instance of the $view variable, this is not the case. event.pm will have $event::view, and icit_messages will have $icit_messages::view, thus the packForget only gets called locally, and when going from one menu item to the other, the previous contents are not forgotten.

    You could either create the $view variable in the main programm, and pass it in to your menu item commands, or instead ask the passed in $top for its children, find out which one is the frame that $view represents, and call packForget on that..

    C.

      So in each module I should replace:
      if ($view){ $view->packForget; }
      with:
      ($view) = grep{$_->name eq 'frame'} $$top->children; if ($view){ $view->packForget; }
      ...?
        However: this doesn't work:
        sub messages_function { my $top = shift; @kids = $$top -> children; foreach (@kids) {print "Name: ", $_->name, "\n";} # if ($view) # { # $view->packForget; # } ($view) = grep{substr($_->name, 0, 5) eq 'frame'} $$top->children; if ($view){ $view->packForget; print "icit_messages: "; print $view; print "\n"; } $view = $$top->Frame(qw/-width 400 -height 400 -background white/) +->pack(); }