Python-uinput当我将其作为脚本运行时,它不能移动光标
python main.py
但是,当我使用Python交互式一步一步地运行它工作正常
import uinput
import Tkinter as tk
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.destroy()
device = uinput.Device([
uinput.BTN_LEFT,
uinput.BTN_RIGHT,
uinput.REL_X,
uinput.REL_Y,
])
def main():
device.emit(uinput.REL_X, -1 * screen_width)
device.emit(uinput.REL_Y, -1 * screen_height)
if __name__ == '__main__':
main()
这是一个不移动光标的命令。
spksoft@spksoft ~/code/python/Sample HI $ python main.py
Traceback (most recent call last):
File "main.py", line 13, in <module>
uinput.REL_Y,
File "/usr/local/lib/python2.7/dist-packages/uinput/__init__.py", line 161, in __init__
self.__uinput_fd = _libsuinput.suinput_open()
File "/usr/local/lib/python2.7/dist-packages/uinput/__init__.py", line 64, in _error_handler
raise OSError(code, os.strerror(code))
OSError: [Errno 13] Permission denied
spksoft@spksoft ~/code/python/Sample HI $ sudo python main.py
spksoft@spksoft ~/code/python/Sample HI $
这是一个python interactive命令,可以正确移动光标。
spksoft@spksoft ~/code/python/Sample HI $ sudo python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import uinput
>>> import Tkinter as tk
>>> root = tk.Tk()
>>> screen_width = root.winfo_screenwidth()
>>> screen_height = root.winfo_screenheight()
>>> root.destroy()
>>> device = uinput.Device([
... uinput.BTN_LEFT,
... uinput.BTN_RIGHT,
... uinput.REL_X,
... uinput.REL_Y,
... ])
>>> device.emit(uinput.REL_X, -1 * screen_width)
>>> device.emit(uinput.REL_Y, -1 * screen_height)
>>>
答案 0 :(得分:1)
至少在X11下,因为你的测试代码突然完成,在代码退出之前,设备的处理程序没有时间创建。
对于代码退出时的这些测试用例,解决方案是添加time.sleep()
命令
在实际运行条件下,不需要这样做。
import uinput
import Tkinter as tk
import time
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.destroy()
device = uinput.Device([
uinput.BTN_LEFT,
uinput.BTN_RIGHT,
uinput.REL_X,
uinput.REL_Y,
])
def main():
time.sleep(1)
device.emit(uinput.REL_X, -1 * screen_width)
device.emit(uinput.REL_Y, -1 * screen_height)
if __name__ == '__main__':
main()