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

Hi All, Whenever i start my little perl/tk program on my Windows desktop (via shortcut) the small main window of my program opens fairly close to the top left of screen, which is not handy...it should appear in the middle of the desktop. Also, after you close the prog and restart it again, the main window now opens several pixels right and down of the previous starting position. I've been looking everywhere for wm geometry examples in Perl, but the search only turns up the Tk::Wm docs, which has no examples. It's easy to do in Tk, but in Perl/Tk i can't get my head around how to use wm properly. Please help.

Replies are listed 'Best First'.
Re: Perl Tk::Wm
by pg (Canon) on Dec 18, 2003 at 05:28 UTC
    use Tk; my $mw = new MainWindow(); $mw->geometry("200x400+0+0"); $mw->deiconify(); MainLoop;

    When a perl/Tk application started, by default the application window gets iconified (appears as an icon). Calling deiconify() overrides the default, and makes the window really displayed,

    geometry() takes the a string as parameter, and the string has the format "wxh[+-]l[+-]t":

    • w: width of the window
    • x: just keep it there
    • h: height of the window
    • +l: margin between the left border of the window and the left end of your screen; -l: margin between the right border of the window and the right end of your screen;
    • +t: margin between the top border of the window and the top end of your screen; -t: margin between the bottom border of the window and the bottom end of your screen;

    Compare the following code with the code at the beginning of this post, tun both of them and see the differences:

    use Tk; my $mw = new MainWindow(); $mw->geometry("400x200-0-0"); $mw->deiconify(); MainLoop;

      Ok, you confused me. I'm not sure I understand the 'deiconify' part, as I've written numerous apps that start up fine and not as an icon by default. Usually my apps look something like this, but never exhibit iconify problems:

      my $win = MainWindow->new; $win->configure(-title =>'hi',-background=>'blue'); $win->geometry('+225+150'); # offset from top left $win->minsize(575,375); # no smaller than this $win->maxsize(575,375); # no larger than this # add widgets ... MainLoop();

      Slightly off-topic: Note that since I want a window that doesn't change size, I don't have to supply the initial width and height to the 'geometry' method. I should, yes, but three calls in a row to control sizes didn't seem quite so elegant. Still, I have two calls, and that seems to be one too many. Can dictating a non-resizable window of dimensions $x and $y be made in a single call?

      As one last follow up: Mastering Perl/Tk by O'Reilly (the Emu book) is a *really* good book. If you can get your work to pay for this, that would be a good thing to do, if not, buy it anyway. I use it all the time and it is really well written. GUI programming books are rarely this easy to read, and this one has tons of content.

        Instead of maxsize and minsize, try $win->resizable(0,0). That specifies whether the window can be resized in width or height (neither in this case). So your code becomes:
        $win->geometry('575x375+225+150'); # geometry / offset $win->resizable(0,0); # non-resizable