0

我正在运行 Ubuntu 12.04 并使用 python 2。我希望能够控制我的鼠标,并且我发现了几个不同的 python 模块打算这样做,但无法让它们工作。

我安装了dogtail,但是当我尝试时:

dogtail.rawinput.click(x,y)

我得到:

AttributeError: 'module' object has no attribute 'rawinput'

然后我尝试了 pymouse,虽然我在导入 pymouse 时使用了 pip 来安装它:

from pymouse import PyMouse

我得到:

TypeError: Object value must be tuple, dictionary or DictWrapper: 0

我也尝试了 uinput 模块,但鼠标命令似乎只在放置在循环中时才起作用,这令人沮丧/荒谬。

如果有人知道如何解决这些问题/我做错了什么/更好或更容易使用模块,我将非常感谢您的帮助。

4

4 回答 4

2

鉴于有限的错误信息,我假设我的问题与您的问题相同。我得到的完整异常和回溯是:

>>> 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

请注意异常发生在 Xlib 中。我安装了 0.15rc1 版本。降级到 0.14 解决了这个问题。

于 2014-04-11T19:49:40.410 回答
1

使用pynput。它可以控制鼠标、键盘等。

例子:

mouse = Controller()
# Set pointer position
mouse.position = (10, 20)
# Press and release
mouse.press(Button.left)
mouse.release(Button.left)
于 2020-03-15T07:29:18.823 回答
0

这是我的一段代码

from pymouse import PyMouse
m = PyMouse()
m.click(654, 169,1) 

X,Y 的前两个参数是定义的最后一个参数单击 1=左键单击 2=右我希望它有所帮助

m.click(x,y,click)
于 2015-02-03T05:08:11.473 回答
0

我建议你安装 pyautogui。您可以控制鼠标和书写。

例子:

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)
于 2020-01-09T09:43:23.210 回答