Hi
I was wondering if there was any way in which using Python i would be able to move the mouse pointer and make it click at certain x and y values. This would be used in a Macro type machine for the mouse. I have looked around for modules that can be used with the mouse but i can't find any at all.
Any help would be greatly appreciated.

Recommended Answers

All 3 Replies

You need to use one of the GUI toolkits for Python. The simplest one is Tkinter that normally comes with Python. You simply add an if statement that does something when your x,y matches the mouse's x,y coordinates (or range):

# show mouse position as mouse is moved and create a hot spot

import Tkinter as tk

root = tk.Tk()

def showxy(event):
    xm = event.x
    ym = event.y
    str1 = "mouse at x=%d  y=%d" % (xm, ym)
    root.title(str1)
    # switch color to red if mouse enters a set location range
    x = 100
    y = 100
    delta = 10  # range
    if abs(xm - x) < delta and abs(ym - y) < delta:
        frame.config(bg='red')
    else:
        frame.config(bg='yellow')
        

frame = tk.Frame(root, bg= 'yellow', width=300, height=200)
frame.bind("<Motion>", showxy)
frame.pack()

root.mainloop()

You can create several hot spots on top of a picture or map and have different text show up.

Formatted solution as:

# show mouse position as mouse is moved and create a hot spot
# based on http://www.daniweb.com/forums/post616327.html#post616327 by ZZucker
import Tkinter as tk

def showxy(event):
    xm, ym = event.x, event.y
    str1 = "mouse at x=%d  y=%d" % (xm, ym)
    # show cordinates in title
    root.title(str1)
    # switch color to red if mouse enters a set location range
    x,y, delta = 100, 100, 10
    frame.config(bg='red'
                 if abs(xm - x) < delta and abs(ym - y) < delta
                 else 'yellow')

root = tk.Tk()
frame = tk.Frame(root, bg= 'yellow', width=300, height=200)
frame.bind("<Motion>", showxy)
frame.pack()

root.mainloop()

And I posted it to http://rosettacode.org/wiki/Mouse_position.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.