http://www.perlmonks.org?node_id=930644

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

Hello!

I need help here to position the tk messagebox

I used tk::messageBox to post a message and the widget was always located in the center of the screen. How can I make it displayed somewhere else, relative to the application main window instead of center of the screen? I used ->place() and it didn't work.

Thanks!

Replies are listed 'Best First'.
Re: tk messagebox position
by keszler (Priest) on Oct 10, 2011 at 15:10 UTC

    Tk::messageBox is a convenient way to pop up a message, but that convenience comes with limits on what you can do. For more control use a Tk::DialogBox. Its Show method is documented with "Any other options supplied to Show are passed to Popup, and can be used to position the Dialog on the screen."

Re: tk messagebox position
by Anonymous Monk on Oct 10, 2011 at 14:25 UTC

    I need help here to position the tk messagebox

    Its a good and smart thing that a messagebox pops up on the center of the screen (or wherever the window manager decides to position it_ -- there is nothing to fix here, move along :)

    How can I make it displayed somewhere else, relative to the application main window instead of center of the screen?

    Search for tk window position, like site:perlmonks.org tk window position and find Tk centering main window on screen.

    I used ->place() and it didn't work.

    Tk::place is a geometry manager, it is used for positioning widgets within a window, it is not used for positioning windows on screen

Re: tk messagebox position
by zentara (Archbishop) on Oct 11, 2011 at 12:25 UTC
    Hi, you will need to make a reusable toplevel window, and repack it with different messages. A few tricks are shown here. The -1-1 geometry placement means "from lower right corner". The toplevel window is reused to prevent memory accumulation from constantly creating new toplevels for each message.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; $mw->title( "MainWindow" ); my $spawn_button = $mw->Button( -text => "Toplevel", -command => \&do_Toplevel )->pack(); my $change_button = $mw->Button( -text => "Toplevel repacked", -command => \&do_Toplevel_repack )->pack(); ######### make a top level withdrawn ################## # make $tl global so it's memory space is reused my $tl = $mw->Toplevel(); $tl->protocol('WM_DELETE_WINDOW' => sub { print "do nothing here\n"; #prevents destruction of $tl #by WM control }); $tl->geometry('300x300-1-1'); $tl->title( "Toplevel" ); $tl->Button( -text => "Close", -command => sub { $tl->withdraw; $spawn_button->configure(-state=>'normal'); $change_button->configure(-state=>'normal'); })->pack(); $tl->withdraw; MainLoop; sub do_Toplevel { $spawn_button->configure(-state=>'disabled'); $change_button->configure(-state=>'disabled'); $tl->deiconify(); $tl->raise(); } sub do_Toplevel_repack { $spawn_button->configure(-state=>'disabled'); $change_button->configure(-state=>'disabled'); #clean out top level my @w = $tl->packSlaves; foreach (@w) { $_->packForget; } $tl->title( "Toplevel repack" ); $tl->geometry('300x500-1-1'); $tl->Button( -text => "Close1", -command => sub { $tl->withdraw; $spawn_button->configure(-state=>'normal'); $change_button->configure(-state=>'normal'); })->pack(); my $text = $tl->Scrolled('Text')->pack(); for (1..100){ $text->insert('end', "$_\n"); $text->see('end'); } #add whatever widgets you want here # Entries, etc $tl->Button( -text => "Add button to mainwindow", -command => sub { $mw->Button(-text=>'new Button')->pack(-side =>'bottom'); })->pack(); $tl->deiconify(); $tl->raise(); }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh