Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Center a Tk window under mouse cursor

by repellent (Priest)
on Nov 03, 2008 at 23:02 UTC ( [id://721239]=perlquestion: print w/replies, xml ) Need Help??

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

How do I center a Tk window under the mouse cursor?

That is, upon invoking MainLoop(), I would like the main window to be centered where the cursor is at.

Update: Thanks, zentara! This is good enough for me:
use Tk; my $main_w = MainWindow->new(); $main_w->title("Test"); my $xpos = $main_w->pointerx; my $ypos = $main_w->pointery; $main_w->geometry("+$xpos+$ypos"); MainLoop();

Replies are listed 'Best First'.
Re: Center a Tk window under mouse cursor
by zentara (Archbishop) on Nov 04, 2008 at 13:21 UTC
    The position of a new window, is usually controlled by an option in your WindowManager. Look thru your WindowManager options for things like "new window at pointer", or similar things. Usually they either center new windows, or try to put it in an empty space.

    About the only thing you can do is drag the window after creation.

    #!/usr/bin/perl use Tk; my $mw = tkinit; $mw->geometry('200x200+200+200'); #$mw->overrideredirect(1); $mw->bind('<ButtonPress-1>', sub{ my $xe = $mw->XEvent; $wdragiinfo{xoff} = $mw->pointerx - $mw->rootx; $wdragiinfo{yoff} = $mw->pointery - $mw->rooty; }); $mw->bind('<B1-Motion>', sub{ my ($x,$y); $x=$mw->pointerx-$wdragiinfo{xoff}; $y=$mw->pointery-$wdragiinfo{yoff}; $mw->geometry($mw->width."x".$mw->height."+$x+$y"); }); MainLoop;

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Center a Tk window under mouse cursor
by Erez (Priest) on Nov 04, 2008 at 07:38 UTC

    I might be 100% wrong here, but the way I understand things, the location of the mouse can only be reported to the GUI after it has been rendered, i.e. after MainLoop is running. What most GUI do to achieve this, is actually move the mouse to the center of the window after it loads. To get it otherwise means you need to ask the OS/WM to tell you where the mouse is at loading time.

    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphors.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://721239]
Approved by pileofrogs
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-25 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found