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

I thought this would be "easy", but I don't seem to have the proper "easy" button. I've entered the following simple snippet (from _Perl in an Nutshell_:
#!/usr/bin/perl -w use Tk; my $mw = MainWindow->new; $button = $mw->Button(-text => "Hello World!", -command =>sub{exit}); $button->configure(-width =>30); $button->pack; MainLoop;
While this works OK, every time I attempt to execute the code (I'm using windoze), the windows pop up in different places (it is a cycle of about 10 different places). What causes this? Can I make it ALWAYS pop up the simple button (main window) in the SAME place. The book isn't that clear on positioning of the main window, thus the seeking of wisdom! Thanks.

Replies are listed 'Best First'.
Re: Follow the window?
by serf (Chaplain) on Jan 18, 2006 at 05:19 UTC
    Yes you certainly can:
    #!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new; my $button = $mw->Button( -text => "Hello World!", -command => sub{exit} ); $button->configure(-width =>30); $button->pack; $mw->geometry("+100+50"); # +X co-ord +Y co-ord MainLoop;
    Note: you can also put WIDTHxHEIGHT before the +x+y if you need to.

    Google:perl tk window position would have helped you here...

    And you will find more (perhaps prettier) examples. e.g. adapted from here:

    use strict; use warnings; use Tk; my $main = MainWindow->new; $main->Label(-text => 'Hello, world!')->pack; $main->Button( -text => 'Quit', -command => [$main => 'destroy'] )->pack; $main->geometry("+100+50"); # +X co-ord +Y co-ord MainLoop;
Re: Follow the window?
by zentara (Cardinal) on Jan 18, 2006 at 14:24 UTC
    Hi, the same thing happens on Linux depending on the window manager you use. Where a window pops up depends on the settings in the window manager. If you don't specify exact coordinates, the window manager "guesses" as to the best place to put it. Some people like "always upper right corner", some like "where the mouse pointer is", etc.

    You can also use negative numbers to specify start from the lower right corner.

    $mw->geometry('100x100-0-0');

    Here is a useful sub to center a window on any screen, as it automatically detects monitor size.

    sub CenterWindow { my($window, $width, $height) = @_; $window->idletasks; $width = $window->reqwidth unless $width; $height = $window->reqheight unless $height; my $x = int(($window->screenwidth / 2) - ($width / 2)); my $y = int(($window->screenheight / 2) - ($height / 2)); $window->geometry($width . "x" . $height . "+" . $x . "+" .$y); }

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