tags:

views:

457

answers:

1

Having more or less converted a lot of old Tk scripts over to Tkx I'm stuck for an port for the following function which repositions the window passed in as a parameter in the centre of the screen. I used to call this just before calling MainLoop, by which point Tk had obviously decided on the reqwidth and reqheight values.

sub CenterWindow
{
    # Args: (0) window to center
    # (1) [optional] desired width
    # (2) [optional] desired height

    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);
}

idletasks can be changed to Tkx::update() if necessary, but I am at a loss to find any obvious translation for the window specific parts of this old Tk routine. Tkx doesn't seem to have an equivalent for reqwidth, reqheight, screenwidth or screenheight retrieveable by cget().

Does the fact that I'm now using a grid layout in Tkx, rather than a pack layout in Tk have any relevance?

BTW I'm running ActivePerl 5.10 on Windows Vista if that makes any difference.

+1  A: 

The geometry manager doesn't matter; that only controls how widgets are laid out within a frame. The data you're after is available via the winfo command:

sub CenterWindow {
    # Args: (0) window to center
    # (1) [optional] desired width
    # (2) [optional] desired height

    my ($window, $width, $height) = @_;

    Tkx::update('idletasks');
    $width  ||= Tkx::winfo('reqwidth',  $window);
    $height ||= Tkx::winfo('reqheight', $window);

    my $x = int((Tkx::winfo('screenwidth',  $window) / 2) - ($width / 2));
    my $y = int((Tkx::winfo('screenheight', $window) / 2) - ($height / 2));

    $window->g_wm_geometry($width . "x" . $height . "+" . $x . "+" . $y);
}

As far as I can tell, you have to invoke winfo directly, you can't use the OO syntax.

You've probably figured this out already, but when developing with Tkx you need to refer to the Tcl Tk documentation. The Tkx documentation just describes how the (very thin) translation layer works. Also, the usenet group comp.lang.perl.tk (or the ptk mailing list bridge to it) is probably the best resource for Tkx questions as at least one of the ActiveState guys behind Tkx can be found there.

Michael Carman