CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [FishMonger] Multi Menu, multi-function Tk Ap: Edit Log



PapaGeek
User

Mar 13, 2014, 9:51 AM


Views: 5297
Re: [FishMonger] Multi Menu, multi-function Tk Ap

OK, another try at you training an old programmer new tricks, making me Perlish!

Here is another set of Perl files. They are far more concise than the previous set.

I changed all of my menu references to window.

I changed the logic so main can call window 1 & 2 and 1 can also call 2. 2 will return properly to whoever called it.

The loop and spaghetti logic will be on me, I want the process to be flexible.

I’m not sure that $child_window-> parent will always work when the child can be called by multiple parents. I created a new Perl file, WindowHash.pl, that controls a hash of references to all windows and also a call stack.

I changed the code to use $window = getWindow('MainWindow')->Toplevel; instead of $window = new MainWindow; but I’m not sure what the difference will be if I do not intend to use the window’s parent logic due to multiple call from options.

I can now open the next window directly using “-command =>[\&ShowWindow, 'Window2']” and return to whoever called this window with “command =>\&PrevWindow”.

This new Perl file does contain two variables that are global within the file, but they are not universal to all other files. Is there a better way to create and handle the hash and array in this file?

Mainline.pl: simple file to load the other files, create the windows and start the loop.

Code
use Modern::Perl '2013'; 
use Tk;

main();

sub main
{
do 'WindowHash.pl';
do 'MainWindow.pl';
do 'Window1.pl';
do 'Window2.pl';

CreateMainWindow();
CreateWindow1();
CreateWindow2();

MainLoop;
}

WindowHash.pl: New logic to manage a hash of window references by name and a call stack of the windows
On Edit: I've change the name of this module to WindowManager.pl since that better describes what it does!

Code
use Modern::Perl '2013'; 
use Tk;

my @WindowStack;
my %WindowHashList;

sub defineWindow
{
my ($windowName, $windowRef) = @_;
$WindowHashList{$windowName} = $windowRef;
}

sub getWindow
{
my ($windowName) = @_;
return $WindowHashList{ $windowName };
}

sub InitWindowStack
{
my ($topWindow) = @_;
push @WindowStack, $topWindow;
}

sub ShowWindow
{
my ($nextWindow) = @_;
my $prevWindow = $WindowStack[-1];
push @WindowStack, $nextWindow;
getWindow($prevWindow) -> withdraw();
getWindow($nextWindow) -> deiconify();
getWindow($nextWindow) -> raise();
}

sub PrevWindow
{
my $thisWindow = pop @WindowStack;
my $prevWindow = $WindowStack[-1];
getWindow($thisWindow) -> withdraw();
getWindow($prevWindow) -> deiconify();
getWindow($prevWindow) -> raise();
}

MainWindow.pl: all windows are now called $window internally. This makes copying controls from one window to another easier. Due to the window hash logic, changing from one window to another can now be done directly by the Button –command =>[\&ShowWindow, 'Window1'].

Code
use Modern::Perl '2013'; 
use Tk;

sub CreateMainWindow
{
my $window;
$window = new MainWindow;

$window->geometry("200x100");
my $lblMsg = $window -> Label(-text=>"Work with sub windows")-> grid();
my $openWindow1 = $window -> Button(-text=>"See Option 1",
-command =>[\&ShowWindow, 'Window1'])-> grid();
my $openWindow2 = $window -> Button(-text=>"See Option 2",
-command =>[\&ShowWindow, 'Window2'])-> grid();
my $endProgram = $window -> Button(-text=>"Exit Program",
-command =>\&endProgram)-> grid();
defineWindow('MainWindow',$window);
InitWindowStack('MainWindow');
}

sub endProgram{
exit;
}

Window1.pl: can perform a task, return to the main menu, and now can also call window 2

Code
use Modern::Perl '2013'; 
use Tk;

sub CreateWindow1
{
my $window;
#$window = new MainWindow;
$window = getWindow('MainWindow')->Toplevel;
$window->geometry("200x200");
my $lblMsg1 = $window -> Label(-text=>"Program Option 1")-> grid();
my $doTask1 = $window -> Button(-text=>"Perform Task 1",
-command =>\&Task1)-> grid();
my $openWindow2 = $window -> Button(-text=>"See Option 2",
-command =>[\&ShowWindow, 'Window2'])-> grid();
my $btnPostpone1 = $window -> Button(-text=>"Return to Main",
-command =>\&PrevWindow)-> grid();
defineWindow('Window1',$window);
$window-> withdraw();
}

sub Task1{
say "doing task 1";
}

Window2.pl: called from both window 1 and the main window. -command =>\&PrevWindow will return to the proper caller.

Code
use Modern::Perl '2013'; 
use Tk;

sub CreateWindow2
{
my $window;
#$window = new MainWindow;
$window = getWindow('MainWindow')->Toplevel;
$window->geometry("200x200");
my $lblMsg2 = $window -> Label(-text=>"Program Option 2")-> grid();
my $doTask2 = $window -> Button(-text=>"Perform Task 2",
-command =>\&Task2)-> grid();
my $btnPostpone2 = $window -> Button(-text=>"Return to Caller",
-command =>\&PrevWindow)-> grid();
defineWindow('Window2',$window);
$window-> withdraw();
}


sub Task2{
say "doing task 2";
}



(This post was edited by PapaGeek on Mar 13, 2014, 2:29 PM)


Edit Log:
Post edited by PapaGeek (User) on Mar 13, 2014, 2:26 PM
Post edited by PapaGeek (User) on Mar 13, 2014, 2:29 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives