954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Tkinter set mouse position?

I've realized that it would be very helpful to me to SET the mouse cursor to a particular location. Having done the usual internet searches, I can't seem to find anything on how to do it. Any ideas?

(So I'm still set on converting to wx, but the project I've just done in Tkinter needs to get finished ASAP. It's an experiment and we need to start running participants.)

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 

You maybe want "focus" or "geometry" depending on what you mean by "mouse position". You can find examples for both of them here http://infohost.nmt.edu/tcc/help/pubs/tkinter/

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

Hmm, I don't think I want either of those things. I want to be able to tell the program to move the mouse cursor to position x, y.

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 

use mouseover or mousefocus at X,Y

sudhakar s
Newbie Poster
1 post since May 2009
Reputation Points: 10
Solved Threads: 1
 
use mouseover or mousefocus at X,Y

We are talking about the Tkinter GUI toolkit. There are no such methods.

widget.focus() puts the cursor to a widget

module turtle (uses tkinter) has a method goto(x,y)

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

Aot,

Did you find a solution?

regards

diego.sueiro
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 1
 

Nope, never found a solution in Tkinter.

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 

I can't think of any practical use for this.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
I can't think of any practical use for this.

I'm a cognitive scientist; I use python to code experiments which often present stimuli to participants that they respond to. If I am timing their responses, sometimes I want very precise control over where the mouse cursor is.

In this particular case, I was making an experiment where participants would see two stimuli, one after another (say a circle and then a bigger circle), and then they would have to rate each on a vertical scale of radio buttons. Because I was timing them, I wanted the mouse cursor to always start in the middle of the scale, so I could time how long it took them to move the cursor from the middle to whichever button they chose.

Originally I just wanted the cursor to disappear during the stimuli presentation, but I couldn't find a way to do that, so I thought perhaps I could simply set the cursor to the center at the start of the radio-button rating.

In the end I just had to ask them not to move their cursor at all while the stimuli were on screen and coded it so that the screen would flash red at them if they did.

So, yeah. It has its uses. :-P

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 

You did not specify what OS you're coding in, but this one is to get the mouse position in Linux, and apparently in the same thread, Gribouillis gives an answer on how to do it in Windows as well. Also read vagaseat's post, which is interesting. Hope this helps.

Dan08
Junior Poster
133 posts since Jul 2009
Reputation Points: 18
Solved Threads: 9
 

Thanks! Although I've long since solved the problem for my particular study and hope to do future studies using wx. It still might come in handy.

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 
You did not specify what OS you're coding in, but this one is to get the mouse position in Linux, and apparently in the same thread, Gribouillis gives an answer on how to do it in Windows as well. Also read vagaseat's post, which is interesting. Hope this helps.

I think the problem is not how to get the mouse position, but how to set the mouse position.

HiHe
Posting Whiz in Training
236 posts since Oct 2008
Reputation Points: 137
Solved Threads: 22
 

Ah yes. I'm sorry, it is about SETTING the mouse position. I read that last post too quickly I see.

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 
from ctypes import *
user = windll.user32
x = 640
y= 480
user.SetCursorPos(x,y)
Tech B
Posting Whiz in Training
268 posts since May 2009
Reputation Points: 59
Solved Threads: 33
 

Thanks! Even if it requires Windows, I appreciate a solution.

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 
from ctypes import *
user = windll.user32
x = 640
y= 480
user.SetCursorPos(x,y)

Tech B,

Is there a way to this in linux?

Regards

diego.sueiro
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 1
 

I'm not running Linux nor have I worked with it besides network cracking via BackTrack.

But I've been told that xorg's xlib could help you out. You'll have to go through its doc's or hit google, but I believe it has the capability's your looking for.

Let me know how it goes.

Tech B
Posting Whiz in Training
268 posts since May 2009
Reputation Points: 59
Solved Threads: 33
 

Tech B,

Is there a way to this in linux?

Regards

Did you find what you where looking for?

Tech B
Posting Whiz in Training
268 posts since May 2009
Reputation Points: 59
Solved Threads: 33
 

Tech B,

Is there a way to this in linux?

Regards


You should experiment with the method warp_pointer in the python-xlib module. (See also XWarpPointer in the xlib documentation). The problem is to find the good arguments to warp_pointer...

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You