Home > Archive > PerlTk > March 2005 > Centering windows on screen
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Centering windows on screen
|
|
| Graham 2005-03-09, 8:57 pm |
| I'm OK with Perl but a Perl/Tk newbie. For good or bad, my bible is
O'Reilly's 'Mastering Perl/Tk'. I can't understand why folk would want
windows to pop up anwhere on screen - usually it's up towards the top left
where mine appear - so I decided I'd like to center them mid-screen. The
O'Reilly doesn't cover it, so I thought I'd install Term::ReadKey v 2.30
thinking that would give me width and height in pixels and I could then do
some math and use the Geometry method to centre my Toplevels mid screen -
something like
use Term::ReadKey;
($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();
# other stuff
$tl = $mw->Toplevel();
$tl->geometry("+($wpixels/2)+($hpixels/2)");
Trouble is Term::ReadKey returns didely squat for pixel size and reading the
documentation it does say that pixel size will only be vaild in some
environments. I'm running Tk v800.024 on a W32 platform.
Am I barking up the proverbial wrong tree here? Is there an easier way?
| |
| Marc Dashevsky 2005-03-09, 8:57 pm |
| In article <422f6e67.0@entanet>, graham@letsgouk.com says...
> I'm OK with Perl but a Perl/Tk newbie. For good or bad, my bible is
> O'Reilly's 'Mastering Perl/Tk'. I can't understand why folk would want
> windows to pop up anwhere on screen - usually it's up towards the top left
> where mine appear - so I decided I'd like to center them mid-screen. The
> O'Reilly doesn't cover it, so I thought I'd install Term::ReadKey v 2.30
> thinking that would give me width and height in pixels and I could then do
> some math and use the Geometry method to centre my Toplevels mid screen -
> something like
>
> use Term::ReadKey;
> ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();
> # other stuff
> $tl = $mw->Toplevel();
> $tl->geometry("+($wpixels/2)+($hpixels/2)");
>
> Trouble is Term::ReadKey returns didely squat for pixel size and reading the
> documentation it does say that pixel size will only be vaild in some
> environments. I'm running Tk v800.024 on a W32 platform.
>
> Am I barking up the proverbial wrong tree here? Is there an easier way?
On February 28 a similar question was asked.
use strict;
use Tk;
my $mw = tkinit;
my $tl = $mw->Toplevel;
$tl->Popup;
MainLoop;
--
Go to http://MarcDashevsky.com to send me e-mail.
| |
| Graham 2005-03-10, 8:56 am |
| $tl->Popup (-popover => undef, -popanchor => "e");
sort of works for me but, depending on the size of the window, it's not
always dead center of the screen
Also, I'm surprised it doesn't work perfectly evey time with -popanchor =>
"c" , but suprisingly with this attribute the window is always over to the
right of the screen
"Marc Dashevsky" <usenet@MarcDashevsky.com> wrote in message
news:MPG.1c993c60a0ee68c9989703@news.comcast.giganews.com...
> In article <422f6e67.0@entanet>, graham@letsgouk.com says...
>
> On February 28 a similar question was asked.
>
> use strict;
> use Tk;
> my $mw = tkinit;
> my $tl = $mw->Toplevel;
> $tl->Popup;
> MainLoop;
>
> --
> Go to http://MarcDashevsky.com to send me e-mail.
| |
| Steve Lidie 2005-03-10, 3:59 pm |
| Graham <graham@letsgouk.com> wrote:
> I'm OK with Perl but a Perl/Tk newbie. For good or bad, my bible is
> O'Reilly's 'Mastering Perl/Tk'. I can't understand why folk would want
> windows to pop up anwhere on screen - usually it's up towards the top left
> where mine appear - so I decided I'd like to center them mid-screen. The
> O'Reilly doesn't cover it,
See page 272 of above O'Reilly book.
| |
| zentara 2005-03-10, 3:59 pm |
| On Wed, 9 Mar 2005 21:45:12 -0000, "Graham" <graham@letsgouk.com> wrote:
Here's another one for your toolkit. :-)
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
my $mw = tkinit;
#centers window
CenterWindow($mw, 300, 200);
MainLoop;
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}");
}
__END__
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
|
|
|
|
|