Hello World!

Being a relative newbie, I was having problems with making Tk Toplevel windows appear over the centre of the main window, and over the centre of the screen. I asked some questions, and got some very complicated answers on how to achieve this, with various geometry commands.

However, today after an inspirational flash, I sorted it all out, so I thought I'd share it.

Spike.

use strict; use warnings; use Tk; my $mw = MainWindow -> new; $mw -> minsize (qw(600 400)); my $button = $mw -> Button ( -text => 'Center of screen', -command => sub {popup('screen')}, ) -> pack; my $other_button = $mw -> Button ( -text => 'Center of main window', -command => sub {popup('window')}, ) -> pack; MainLoop; sub popup { my $type = $_[0]; my $popup = $mw -> Toplevel; $popup -> withdraw; for (1..10) { my $label = $popup -> Label ( -text => "This is a label $_", ) -> pack; } if ($type eq "screen") { $popup -> Popup(); } else { $popup -> Popup ( -popover => $mw, -overanchor => 'c', -popanchor => 'c', ); } }
UPDATE:
(For Win32 systems)
If you use the minsize command on the window that you want to popup, you will discover that it does not get positioned properly. This is due to a bug in the Popup method.

This bug will be fixed in the next release, but if you want to fix it now, then add the marked 3 lines to the Popup subroutine in the following files: C:\Perl\site\lib\auto\Tk\Wm\Popup.al and C:\Perl\site\lib\Tk\Wm.pm

sub Popup { my $w = shift; $w->configure(@_) if @_; $w->idletasks; my ($mw,$mh) = ($w->reqwidth,$w->reqheight); my ($minwidth,$minheight) = $w->minsize; #ADD $mw = $minwidth if (defined $minwidth and $minwidth > $mw); #ADD $mh = $minheight if (defined $minheight and $minheight >$mh); #ADD my ($rx,$ry,$rw,$rh) = (0,0,0,0); ...

In reply to Tk, and positioning popup windows. by spikey_wan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.