2012-03-13 52 views
2

例如,如果我想使用類似:如何從python模塊/腳本中使用xdotool?

xdotool鼠標移動945 132

xdotool點擊1

爲了把鼠標移動到某個位置,然後單擊。在Ubuntu中,我只需將這些命令直接鍵入終端即可獲得所需的效果,但我希望將它們放入Python腳本中。提前致謝!

回答

8
import subprocess 

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

等見subprocess文檔。

+0

真棒!!非常感謝! :d – coffeeNcode 2012-03-13 10:22:04

0

我一直在使用xdotool與SH和使用os.system一段時間,但決定更新一切使用子進程。這樣做,我遇到了一些小問題,並在谷歌搜索the libxdo python module suggested by Simon。有與Python3一個小問題 - 它使用的字節串 - 但轉化爲簡單,運行更平穩,可靠,舊兩個步驟的過程。

這裏有一個小的代碼,可以幫助(顯然哈希爆炸會需要匹配你的Python路徑)。這兩個函數包括轉化爲字節串(ASCII)爲Python 3,從而.encode()可以離開用於Python 2.

#!/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') 
相關問題