问题
I'm running Ubuntu 12.04 and working with python 2. I would like to be able to control my mouse, and I have found several different python modules intended to do this, but cannot get them to work.
I installed dogtail, but when I try:
dogtail.rawinput.click(x,y)
I get:
AttributeError: 'module' object has no attribute 'rawinput'
I then tried pymouse and although I used pip to install it when I import pymouse:
from pymouse import PyMouse
I get:
TypeError: Object value must be tuple, dictionary or DictWrapper: 0
I tried the uinput module as well but the mouse commands only seemed to work when placed in a loop, which is frustrating/ridiculous.
If anyone knows how to fix either of those problems/what I'm doing wrong/better or easier to use modules I would much appreciate the help.
回答1:
I'll assume that my problem is the same as yours, given the limited error information. The full exception and traceback I got was:
>>> import pymouse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pymouse/__init__.py", line 95, in <module>
from unix import PyMouse, PyMouseEvent
File "/usr/local/lib/python2.7/dist-packages/pymouse/unix.py", line 53, in <module>
class PyMouseEvent(PyMouseEventMeta):
File "/usr/local/lib/python2.7/dist-packages/pymouse/unix.py", line 66, in PyMouseEvent
'client_died': False,
File "/usr/local/lib/python2.7/dist-packages/Xlib/ext/record.py", line 121, in create_context
ranges = ranges)
File "/usr/local/lib/python2.7/dist-packages/Xlib/protocol/rq.py", line 1403, in __init__
self._binary = apply(self._request.to_binary, args, keys)
File "/usr/local/lib/python2.7/dist-packages/Xlib/protocol/rq.py", line 1088, in to_binary
return apply(self.to_binary, varargs, keys)
File "<string>", line 3, in to_binary
File "/usr/local/lib/python2.7/dist-packages/Xlib/protocol/rq.py", line 523, in pack_value
data.append(self.type.pack_value(v))
File "/usr/local/lib/python2.7/dist-packages/Xlib/protocol/rq.py", line 1102, in pack_value
return apply(self.to_binary, (), value)
File "/usr/local/lib/python2.7/dist-packages/Xlib/protocol/rq.py", line 1088, in to_binary
return apply(self.to_binary, varargs, keys)
File "<string>", line 4, in to_binary
File "/usr/local/lib/python2.7/dist-packages/Xlib/protocol/rq.py", line 573, in check_value
vals.append(f.check_value(val[i]))
File "/usr/local/lib/python2.7/dist-packages/Xlib/protocol/rq.py", line 582, in check_value
raise TypeError('Object value must be tuple, dictionary or DictWrapper: %s' % val)
TypeError: Object value must be tuple, dictionary or DictWrapper: 0
Note the exception occurs in Xlib. I had version 0.15rc1 installed. Downgrading to 0.14 fixed the problem.
回答2:
use pynput . It can control mouse, keyboard, etc.
examples:
mouse = Controller()
# Set pointer position
mouse.position = (10, 20)
# Press and release
mouse.press(Button.left)
mouse.release(Button.left)
回答3:
this is my piece of code
from pymouse import PyMouse
m = PyMouse()
m.click(654, 169,1)
the first two arguments ar for X,Y the last argument to define wich click 1=left click 2=right i wish it helps
m.click(x,y,click)
回答4:
I recomend you install pyautogui. You can control mouse and write.
Example:
import pyautogui as py
py.move(x,y) #it moves the mouse
py.click(x,y) #click the mouse on a position
py.write('write this') #Write
py.press('enter') #press a key (enter in this case)
来源:https://stackoverflow.com/questions/16890195/mouse-control-with-python