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

Consider the following code:
use Tk; my %font=(-font=>[-family=>'Courier New',-size=>10]); my %width=(-width=>120); my $mw=new MainWindow; my $entry=$mw->Entry( %width, -relief=>'groove', -borderwidth=>5, -textvariable=>\$entry_text, %font )->pack; $entry->focus; $entry->bind('<Key-Return>',\&enter); my $text=$mw->Text( %font, %width, -height=>43 )->pack; #$mw->update; my $geom=$mw->geometry; print "geom: $geom\n"; $geom=~s/\+\d+\+\d+/+5+5/; $mw->geometry($geom); MainLoop; ################ prints: geom: 1x1+0+0
This creates a quite sizeable main window in Tk. I would like to position this window exactly at screen coordinates (5,5) to be fully visible. The idea is to query the geometry of the main window, replace the (x,y) coordinates with a regular expression and then set it again with the correct geometry string. However before invoking the main loop $mw->geometry returns '1x1+0+0'. Unly updating the window (commenting out #$mw->update;) builds the geometry string in the correct way. This results in the main window appearing in the wrong place and then instantly being removed which causes an unfavourable flickering. (Iconifying before and deiconifying after does not help!) Any thoughts on how to get the correct dimensions of the window without displaying it?

Replies are listed 'Best First'.
Re: MainWindow geometry problem (update?)
by BrowserUk (Patriarch) on Feb 10, 2012 at 05:45 UTC

    Why not just set the top left position and let it work the rest out for itself:

    ... $mw->geometry('+5+5'); MainLoop;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Somehow I had the obsession that you can't set the geometry string partially. Your solution works perfectly. Thanks.
Re: MainWindow geometry problem (update?)
by Khen1950fx (Canon) on Feb 10, 2012 at 07:39 UTC
    Try this. I did the geometry a little bit differently.
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Entry; my $mw = MainWindow->new; $mw->geometry("150x75+250+250"); $mw->title("Entry"); my $entry = $mw->Entry( -text => "Entry: ", -background => "black", -foreground => "green", -font => "courierfont 10 bold", -relief => "groove", -width => 30, -selectborderwidth => 5 )->pack( -expand => 1, -fill => "x", -ipadx => 10, -ipady => 10 ); $mw->Button( -text => "Quit", -command => ["destroy", $mw] )->pack(-side => "bottom"); $entry->bind("<Key-Return>", "\033"); $mw->update; MainLoop;