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

I would like to use a more 'Windows' style of GUI in a Perl programme.

Specifically I need to be able to open new windows and close them during normal operation. For example, I want to set up an SQL query in one window, then show the result in another window.

I have only a passing knowledge of Tk or Perl/Tk, but a good working knowledge of Perl in Web-Apps and CGI.

Pointers to modules youhave succesfully used, or tutorials would be espcially useful!

John

Replies are listed 'Best First'.
Re: Multiple Windows in Perl/Tk
by bobn (Chaplain) on Jul 16, 2003 at 22:33 UTC

    In general, you will want to get the book "Mastering Perl/Tk".

    When you create the new window, you'll have a 'close' button or menu entry on it. If you have to create this (e.g. there sin't a module that will do this automagically), then you'll need to give this button a callback containing something that will reference the window. In this case, use a closure in the callback to hold the window object for use in destroy():

    #!/usr/bin/perl -w use Tk; my $mw = MainWindow->new(-title=>"Demo"); my $HlpBttn = $mw->Button(-text=>"NEW", -command=> sub { make_win(); }); $HlpBttn->pack(); MainLoop; sub make_win { my $win = $mw->Toplevel(-title=>'new window', -height=>10, -width=>50); my $Bttn = $win->Button(-text=>"CLOSE", -command=> sub { close_win($win); } )->pack; } sub close_win { my $thiswin = $_[0]; $thiswin->destroy; }

    The callback in each window has it's own instance of $win locked up in the coderef, ( -command => sub { close_win($win) } ), which retains the value $win had at the timew the window was created, so it "knows" which window to destroy when called. This coderef carrying around an instance of a lexical variable from the coderef's enclosing scope is called a "closure".

    Update: Created a minimal example, got rid of all the old stuff.

    --Bob Niederman, http://bob-n.com
      Bob, I set about looking at Mastering Perl/Tk more intensely after your reply. What I needed to read was the section commencing on page 238 which talks about Methods on the Toplevel widget.

      Thanks for the clues.

      John
Re: Multiple Windows in Perl/Tk
by graff (Chancellor) on Jul 16, 2003 at 20:48 UTC
    A single Perl/Tk app can create any number of "MainWindow" objects, and any number of "Toplevel" windows -- both are handled by the user's main window manager. (Check the corresponding man pages to see what's different and common among these two types of widgets.)

    You can associate create, destroy, iconify and de-iconify (aka "unmap" and "map") operations with whatever user controls seem appropriate (buttons, menu options, etc), so that windows appear and disappear whenever the user decides; controlling geometry requires somewhat more attention to detail, but is certainly possible in unix/x-windows environment (I'm not sure about ms-windows, but it should work there as well).

Re: Multiple Windows in Perl/Tk
by bioinformatics (Friar) on Jul 16, 2003 at 21:00 UTC
    I don't know if you're looking for a book, but there is a book that I'm reading that seems to do a good job of explaining Perl/Tk-Advanced Perl Programming by Sriram Srinivasan. Also, you might check out this url: http://www.perl.com/pub/a/1999/10/perltk/. Hope that helps a little...

    Daniel
      I have the book here! I have been trying to work from "Mastering Perl/Tk" but I have fi\ound it to be not so good!

      Every Perl book published by O'Reilly is on my shelf, so I will go get "Advanced Perl" and have a read, thank you!

      John