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

I need to display a message box after a user clicks on a button. For some reason, I can't seem to get this code to work. Any help is much appreciated.

The text "before\n" is printed, but the dialog is not displayed.

#! /usr/bin/perl -w # use strict; use warnings "all"; use Tk; use Tk::Dialog; our $mw = new MainWindow; my $f_select = $mw->Frame( -bd=>2, -relief=>'groove') ->pack(-side=>'l +eft', -fill=>'y'); my $f_refresh = $f_select->Frame() ->pack(-side=>'bottom', -fill=>'x') +; $f_refresh->Button(-text => 'Test', -command => sub {displayMsg()}) -> +pack(-side=>'left'); # This one works. $mw->Dialog(-title => "Test", -text => "My test Message", -bitmap=>'qu +estion', -buttons => ['Ok'])->Show(); MainLoop; exit; # Should never actually get here. sub displayMsg { print "before\n"; # for some reason, Dialog() does not work from this context. # $mw->Dialog(-title => "Error", -text => "My Error Message", -bit +map=>'error', -buttons => ['Ok'])->Show(); my $dialogWindow = $mw->Dialog(-title => "Error", -text => "My Err +or Message", -bitmap=>'error', -buttons => ['Ok']); $dialogWindow->Show(); # neither does messageBox :-( #my $answer => $mw->messageBox(-title => 'Please Reply', # -message => 'Would you like to continue?', # -type => 'YesNo', -icon => 'question', -default => 'yes'); print "after\n"; }

Replies are listed 'Best First'.
Re: Can't get Dialog or messageBox to work from button
by Anonymous Monk on Jan 11, 2016 at 13:26 UTC
    Your problem seems to be at another place in mind. This is a never ending loop between two buttons. At every reach the windows moves to the left, like a negative offset. You may place a definition of screen and position inside...
      Thanks :-) With your help I was able to find the missing piece. Here is the working code:
      my(@popup_opts) = (-popover => undef, qw/-overanchor sw -popanchor nw/ +); my $dialogWindow = $mw->Dialog( @popup_opts, -title => "Error", -text => "My Error Message", -bitmap=>'erro +r', -buttons => ['Ok']);
      This is supposedly documented under Tk::Popup, according to http://search.cpan.org/dist/Tk/pod/DialogBox.pod, but I can't seem to find Tk::Popup documentation.

      Well, this is almost solved. I have an external monitor attached to my MacBook Pro. When I run my example it runs on the laptop monitor (which is on my right). And the dialog shows up on the laptop monitor on the top left of the screen. However, if I first move the main application to the monitor, then when I press the button, the Dialog is nowhere to be found! I can't find it on either monitor.

      Does anybody know where the documentation for Tk::Popup is at? Or know what I can do to get the dialog displayed onscreen in this case?

Re: Can't get Dialog or messageBox to work from button
by hotchiwawa (Scribe) on Jan 11, 2016 at 15:18 UTC
    Try with:
    $f_refresh->Button(-text => 'Test', -command => sub { &displayMsg()})- +>pack(-side=>'left');
    Call all your functions in Tk with the & or \& prefix if they are defined after their call. Perl can't find them. ;)

    Edit
    Some call examples:

    Here inside an anonymous subroutine: end_automode and ping_site are called.
    $update_bttn = $button_f->Button(-text => "Update", -state => 'disabled', -command => sub { &end_automode; &ping_site });
    Here a direct call to the subroutine do_automode, bind to the command.
    $automode_bttn = $button_f->Button(-text => "Start Automode", -command => \&do_automode);
    Rem: end_automode, ping_site, do_automode are defined after their calls.
      Sure!
      sub xx { shift() + 1 } say \&xx(3); # SCALAR(0x60003b660) say ${ \&xx(3) }; # 4 say sub { \&xx(shift) }->(3); # SCALAR(0x60003b888) say ${ sub { \&xx(shift) }->(3) }; # 4
      How much easier than
      say xx(3); # 4 say sub { xx(shift) }->(3); # 4
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        I don't use Tk and therefore i didn't really read the friendly manual but i would like to mention that i figured out in a hurry that the construct hotchiwawa mentions can be found here and here. Perhaps he jumped to the wrong conclusion?

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

      Can you please explain that. Why would anyone "Call all your functions with the \& prefix" and what does it have to do with "they are under ;)"?
        I added info in the post.