如何在python模块/脚本中使用xdool?

2024-10-06 10:30:35 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,如果我想使用如下内容:

xdotool鼠标移动945 132

xDool单击1

以便将鼠标移动到某个位置并单击。在ubuntu中,我可以直接在终端中输入这些命令来获得想要的效果,但是我想把它们放在Python脚本中。提前谢谢!


Tags: 命令脚本终端内容ubuntu鼠标效果xdotool
3条回答
import subprocess

subprocess.call(["xdotool", "mousemove", "945", "132"])

等等。请参阅^{}文档。

从2015年起,您还可以使用此python包: https://github.com/rshk/python-libxdo

我在sh和os.system中使用xdool已经有一段时间了,但我决定更新所有内容以使用子流程。这样做的时候,我遇到了一些小问题,在google上发现了the libxdo python module suggested by Simon。Python3有一个小问题——它使用bytestrings——但是转换很简单,而且运行起来比以前的两步过程更平稳可靠。

这里有一些代码可能会有帮助(显然散列爆炸需要匹配您的python路径)。这两个函数包括对Python 3的bytestrings(ascii)的转换,因此对于Python 2,可以不使用encode()。

#!/home/john/anaconda3/bin/python3.6
import sys
from xdo import Xdo
from time import sleep

def sendkeys(*keys):
    for k in keys: xdo.send_keysequence_window(0, k.encode())

def type(text):
    xdo.enter_text_window(0, text.encode())

sleep(0.5)
xdo = Xdo()

# this updates a row in a spreadsheet with copies from prior row
# first check that this is the intended spreadsheet
if 'Trades' in xdo.get_window_name(xdo.get_active_window()).decode():
    with open('my_data_file_name', 'r') as f:
        trade = (f.readlines()[-int(sys.argv[1])])[:-1]
        t = [s if s else '0' for s in trade.split('\t')]
        type('\t'.join(t[:7]))
        sendkeys('Tab', 'Up', 'ctrl+c', 'Down', 'ctrl+v', 'Right')
        type(' ' + t[-3])
        sendkeys('Tab')
        type(t[-2])
        sendkeys('Tab')
        type(t[-1])
        sendkeys('Tab', 'Up', 'ctrl+c', 'Down', 'ctrl+v', 'Right')
        type('333')
        sendkeys('Tab')

相关问题 更多 >