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

Hi Guys Does anyone know if itīs possible to add an icon to the window decoration of a messagebox? I'm using the following code, but the icon in the top left corner of the messagebox - just looks like a standard .exe icon.
$MW->messageBox(-message => $message, -icon => 'question', -type => 'okcancel', -title => '$title');
Any hints? Regards Schimkat

Replies are listed 'Best First'.
Re: Missing TK Messagebox icons
by thundergnat (Deacon) on Nov 15, 2007 at 20:06 UTC

    If I understand you correctly, no, you can't easily change the default icon in the title bar of a messageBox. Mostly because the messageBox megawidget will auto display as soon as it is created, not allowing you any time to modify the window managers defaults.

    You can however, use a Dialog (which messageBox is based on), since it is not automatically displayed as soon as it is created. It takes a little more work, but allows greater flexibility.

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Dialog; my $top = MainWindow->new; # a 32x32 gif image MIME64 encoded my $icon = $top->Photo(-format => 'gif', -data => ' R0lGODlhIAAgALMAACcREeOxI1lSTJ6XlW5padXNvLOspYF5cnFoV5KKfdnX1j44Ntq9Xv +v8+vjy wL65uiH5BAAAAAAALAAAAAAgACAAAwT/sMlJq7046837VsUjjkY5FkraPaUxigZBmLARav +CQlEM/ HIeeYWBIJISKi6KlOw5GB4JzaCTalM/iAUGw/ggCaeIwdg2VVHK1FwXLEGPjcIDWAoEyAv +eNuAeJ GQ89RwiFAgsCAgiJiYUHJUkZBS1ai4yFcEY7BR0gRQlgh41kT5EeFAQAqgIFKKcZqauvGw +eqAAKz DAwOFwK2BK8OAQEMF7G3F4KcFcLDvb8WCbZ0FQzEFg8LCwDaphIGtgkWRbwTCqAEeIkHFb +UGyQvl LF8+R4dEkX0YD2T8fXc+yICJougHAgwDRCFC1IWNjCjabvU5uE9TooHp3iTStsDIAW8UZg +ro2ILJ IyZFmBwZQWhkoko4ZAxtVATkwYUB6frokRIET5tQjRAYMKUAp4EohtLJERiF5KVFC6iBWU +BAgYyg OVNa0taI44IkAiDi5BqUqiI9jBZKo7rgXUKgjAYSPMQ1IiK62thFAAA7 '); # set up the dialog my $dialog = $top->Dialog( -bitmap => 'warning', -text => 'Danger Will Robinson!', -title => 'Robot Says', -buttons => [qw(Run! Flee! Panic!)], ); # Modify the default icon $dialog->Icon(-image => $icon); # display it my $answer = $dialog->Show; print $answer; MainLoop;
      It would seem that you were the only one understanding my question, and your example worked just fine. :-) Compared to the Messagebox, the dialog looks quite ugly, but I guess that I just need to do some fine tuning with the fonts and bitmaps. Regards Schimkat
      You can however, use a Dialog (which messageBox is based on)
      This is only true for X11 systems, but AFAIK on Windows the messageBox is a native Windows dialog.

      In Tk804.027_501 there's a new Tk::MsgBox, which looks slightly less ugly and where your ->Icon trick also works.

Re: Missing TK Messagebox icons
by zentara (Cardinal) on Nov 15, 2007 at 14:54 UTC
    You will probably need to make your own icon if on Win32.
    #!/usr/bin/perl use warnings; use strict; use Tk; # from perldoc messageBox # -icon # Specifies an icon to display. On X11 any of the # builtin Tk bitmaps can specified. On Windows only # error, info, question or warning are supported. my $mw = tkinit; my $bits = pack("b8"x8, "...11...", "..1111..", ".111111.", "11111111", "11111111", ".111111.", "..1111..", "...11...",); $mw->DefineBitmap('indicator' => 8,8, $bits); $mw->Button(-text=>'Press Me', -command => sub{ my $message = localtime; &send_message( $message,'indicator','green') } )->pack; $mw->Button(-text=>'Press Me 1', -command => sub{ my $message = localtime; &send_message( $message,'error','red') } )->pack; MainLoop; sub send_message{ my ($message,$icon, $color) = @_; my $mb = $mw->messageBox( -background => 'lightyellow', -foreground => $color, -icon => $icon, -message => $message, -type => 'OK' ); }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Missing TK Messagebox icons
by radiantmatrix (Parson) on Nov 15, 2007 at 17:12 UTC

    I'm not familiar with messageBox, but many similar Tk widgets will take any Image as a valid parameter to -icon.

    So, something like:

    my $msg_icon = $MW->Photo( -file=>'msgicon.xpm' ); $MW->messageBox( -message => $message, -icon => $msg_icon, -type => 'okcancel', -title => $title );

    Ought to work. See Tk::Photo, which is an Image widget.

    <radiant.matrix>
    Ramblings and references
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
      Ought to work

      Don't confuse the icon shown when a window is minimized, with an icon meant for informational display. If you read perldoc Tk::messageBox, it says it needs a Tk bitmap. Perldoc Tk::Bitmap specifies

      -data => string Specifies the contents of the source bitmap as a string. The string must adhere to X11 bitmap format (e.g., as generated by th +e bitmap program). If both the -data and -file options are specified,the -data option takes precedence. -file => name name gives the name of a file whose contents define the source bi +t map. The file must adhere to X11 bitmap format (e.g., as gener +ated by the bitmap program).
      So all Tk::Photo objects will not work.

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum