Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Center a Tk window on the screen

by bbfu (Curate)
on Jan 04, 2003 at 03:29 UTC ( [id://224185]=CUFP: print w/replies, xml ) Need Help??

This function centers a Tk::Toplevel on the screen, taking into account the window's width and height.

Caveat: To get the correct window size, the function has to force the window to update, which causes the window to be displayed if it wasn't. If you want the window to end hidden, leave out the deiconify, near the end.

sub center { my $win = shift; $win->withdraw; # Hide the window while we move it about $win->update; # Make sure width and height are current # Center window my $xpos = int(($win->screenwidth - $win->width ) / 2); my $ypos = int(($win->screenheight - $win->height) / 2); $win->geometry("+$xpos+$ypos"); $win->deiconify; # Show the window again }

Replies are listed 'Best First'.
Re: Center a Tk window on the screen
by zentara (Archbishop) on Jan 04, 2003 at 15:32 UTC
    Here's something to try, it centers as well:
    #!/usr/bin/perl use Tk; use strict; my $mw=tkinit; $mw->withdraw; #avoid the jumping window bug $mw->Popup; MainLoop;

      Interesting! Can you point me to where the Popup() method is documented?

      Black flowers blossum
      Fearless on my breath

        Hmm, Mastering Perl/Tk has a section on it in Chapter 12. Otherwise, a good source of info is doing a search for "tk Popup methods"
Re: Center a Tk window on the screen
by {NULE} (Hermit) on Jan 04, 2003 at 15:05 UTC
    Sweet. How coincidental - I was pondering exactly this the other day. I've been rewritting the old windows video-game Gazillionaire ( in Perl/Tk for my own amusement recently and I was thinking about how to do a fullscreen mode using just Tk. Between this and your other node on removing decorations, this is perfect!
    $w->{main}->overrideredirect(1); $w->{main}->geometry($w->{main}->screenwidth."x".$w->{main}->screenwid +th."+0+0");

      Of course, if you're running it in MS win32, you should be aware that any "stay on top" windows (such as the taskbar and, on my computer anyway, Trillian) will still be in front. You'll have to do something using Win32 or Win32::API to move them to the back, probably. Although, it's possible Tk has some way to force a z-order for your window (I haven't checked).

      Black flowers blossum
      Fearless on my breath

        Yeah - that's fine. I was going to put something in the help about how you could turn autohide on if you want to use fullscreen. At any rate, it's nice to have the option, for those that don't mind tweaking things a bit to use it.

        This is what I ended up using and it works well, excluding the stay-on-top thing (tested on SuSE 8.0 (5.6.1) and Win98 (ActiveState-5.8.0):

        if ($s->{fullscreen}) { $s->{fullscreen} = 0; $w->{main}->withdraw; $w->{main}->update; $w->{main}->overrideredirect(0); $w->{main}->geometry($s->{windowed}||"800x600+20+20"); $w->{main}->deiconify; $w->{main}->update; } else { $s->{fullscreen} = 1; $s->{windowed} = $w->{main}->geometry; $w->{main}->withdraw; $w->{main}->update; $w->{main}->overrideredirect(1); $w->{main}->geometry($w->{main}->screenwidth."x".$w->{main}->scree +nheight."+0+0"); $w->{main}->deiconify; $w->{main}->update; }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://224185]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-06-19 13:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.