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

Hi Monks :)

I have a problem. I want to show a messagebox when program starts if something's wrong. I do it before mainloop. after clicking ok the program freezes! how can i show a messagebox at startup? i cant do it after calling to mainloop (errors).
Maybe timers could help but i don't like that solution. can anybody help?

thaks :)
#/usr/bin/perl use Tk; #sub msgb; $mw = MainWindow->new; $mw->Button(-text => "hello", -command => sub {print "hello!\n";})->pa +ck; my $test = 0; if ($test == 0) { my $answer = $mw->messageBox(-icon => 'question', -message => 'Hel +lo World!', -title => 'My title', -type => 'Ok'); } MainLoop;

holli fixed formatting

Replies are listed 'Best First'.
Re: MessageBox before mainloop problem
by GrandFather (Saint) on Aug 30, 2005 at 09:40 UTC

    When I run your code as posted, but changing $test to be 1 I get two windows opened. One with the title "My title" and an OK button. The other is titled "Noname" and has a hello button. Clicking OK dismisses the "My title" window and leaves the "Noname" window. The "Noname" window is not active until the "My title" window is closed.

    Could it be that your "My title" window is hidden behind another window and that is why you program seems to freeze?

    My testing was with Windows XP


    Perl is Huffman encoded by design.
      I ran this code on Linux and the exact same thing happend as GrandFather described, but I did not change test to be '1'... I just left it at '0'.

      Go Fish!

Re: MessageBox before mainloop problem
by zentara (Cardinal) on Aug 30, 2005 at 10:57 UTC
    Just put a 0 or 1 as a commandline arg to start the script. It defaults to 0.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $test = shift || 0; my $mw = MainWindow->new; $mw->withdraw; $mw->Button(-text => "hello", -command => sub {print "hello!\n";})->pack; if ($test == 0) { my $answer = $mw->messageBox(-icon => 'question', -message => 'Hello World!', -title => 'My title', -type => 'Ok'); $mw->deiconify; }else{ $mw->deiconify; } MainLoop;

    I'm not really a human, but I play one on earth. flash japh
      Hi,
      Can you click the hello button after closing the messagebox?
      me not (perl 5.6). I sent a shortened code to show you what
      i was talking about. It doesn't matter what it defaults to!
      It is zero to show the messagebox, otherwise it works.
      
      Vikee
      
        It works in 5.8, althougth the Tk version is probably more important. I can't see why the button wouldn't work for you. I changed the code to count up with each click.
        #!/usr/bin/perl use warnings; use strict; use Tk; my $test = shift || 0; my $mw = MainWindow->new; $mw->withdraw; my $count = 0; $mw->Button( -text => "hello", -command => sub { print $count++,"\n"; } )->pack; if ( $test == 0 ) { my $answer = $mw->messageBox( -icon => 'question', -message => 'Hello World!', -title => 'My title', -type => 'Ok' ); $mw->deiconify; } else { $mw->deiconify; } MainLoop;

        I'm not really a human, but I play one on earth. flash japh
Re: MessageBox before mainloop problem
by jdporter (Paladin) on Aug 30, 2005 at 12:53 UTC
    I would just point out that it is possible to call MainLoop more than once in a program. You could use the MainWindow for a message box, and call MainLoop. When the user closes that window, MainLoop exits. Then you can re-construct the MainWindow for the "main" app, and call MainLoop again.