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

Dear Monks

What I am doing wrong in this script? If I clic on the X button, the window won't close...

use warnings; use strict; use Tk; use Tk::Dialog; my $mw = MainWindow->new(); my $dialog_setup = $mw->Dialog(-title => "My Window", -background=>"wh +ite",-buttons => [qw/Ok Quit/]); my $frame0 = $dialog_setup->Frame (); $frame0->pack(-side => 'top', -expand => 'both', -fill => 'x'); $frame0->Label(-background=> 'white', -text => "This is my window", -b +ackground=>"white")->pack(-side => "left", -anchor => "w",-fill => "b +oth",-expand => "x"); my $answer = $dialog_setup->Show(); if ($answer=~ /Ok/){ do_something();} MainLoop();

Thank you for your advice

Replies are listed 'Best First'.
Re: tk close window
by kcott (Archbishop) on Nov 07, 2010 at 10:58 UTC

    Well, there's no X button.

    The Ok button throws an exception: Undefined subroutine &main::do_something called ....

    The Quit button closes the Dialog window.

    Did you think the Quit button shut down the entire application? It doesn't.

    Here's an Exit button you can add:

    $mw->Button(-text => 'Exit', -command => sub { exit })->pack();

    I added that just before my $answer = ... and successfully tested it.

    -- Ken

      Sorry Ken, I express myself wrongly. With X button I mean the (OS Windows)button you see on the top left of the window(togheter with minimize and risize)

        You start a Tk::Dialog before you enter MainLoop with $dialog_setup->Show().

        Show() has two modes: local grab and global grab. Without arguments, as you've used, the mode is local grab.

        With a local grab, the widget which is the parent of the Dialog ($mw in this case) is effectively frozen - you need to close the Dialog widget before you can perform any operations on the parent. It's normal to start a Dialog after you enter the MainLoop (e.g. Menu-Help-About typically shows "About" information in a Dialog or similar widget).

        In a couple of my tests (on Windows XP and Cygwin), the Dialog was minimised and the "frozen" main window was displayed - I suspect this might have been the behaviour you were seeing. I haven't seen that before but I've always started Dialogs after the MainLoop has been entered. Take a look at the widget demo (Common Dialogs - Message boxes) on your system to check how it "normally" operates.

        -- Ken

Re: tk close window
by Khen1950fx (Canon) on Nov 07, 2010 at 14:47 UTC
    I tried it with Tk::DialogBox. I added a cancel_button and a sub to exit.
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::DialogBox; require Tk::Dialog; my $mw = MainWindow->new; my $d = $mw->Dialog( -title => "My Window", -background => "white", -cancel_button => "Quit", -buttons => [qw/Ok Quit/]); my $frame0= $mw->Frame (); $frame0->Label( -text => "This is my window", -background => "white")->pack; my $answer = $d->Show(); Exit(); MainLoop(); sub Exit { exit 0; }
Re: tk close window
by PeterPeiGuo (Hermit) on Nov 07, 2010 at 17:08 UTC

    Let's back off. This code worked on windows 7, with strawberry perl 5.12.1 and Tk 804.029.

    It worked in the sense that there were two (not one) X buttons - one for the dialog and one for the main window, and both responded to click event.

    I think you need to tell more about your environment, so someone with a similar onfiguration can try for you.

    Peter (Guo) Pei

      I'm running the script on XP and Perl 5.8.8

      I found the following work around (using protocol('WM_DELETE_WINDOW'):

      use warnings; use strict; use Tk; use Tk::Dialog; my $mw = MainWindow->new(); my $dialog_setup = $mw->Dialog(-title => "My Window", -background=>"wh +ite",-buttons => [qw/Ok Quit/]); $dialog_setup->protocol('WM_DELETE_WINDOW', \&destroy_dialog); my $answer = $dialog_setup->Show(); if ($answer=~ /Ok/){ do_something();} MainLoop(); sub destroy_dialog { $dialog_setup ->destroy; }
Re: tk close window
by Anonymous Monk on Nov 07, 2010 at 11:51 UTC
    It works for me, Tk: 804.029, perl v5.10.1 built for MSWin32-x86-multi-thread