讓透過使用者預先定義的動作,操作滑鼠。
program.py (使用 pyautogui)
import pyautogui
import time
# 讀取設定檔案
with open("settings.txt", "r") as settings_file:
lines = settings_file.readlines()
# 解析每一行指令
for line in lines:
# 移動到指定位置
if line.startswith("moveTo"):
_, x, y = line.split()
pyautogui.moveTo(int(x), int(y), 0.5)
# 點擊
elif line.startswith("click"):
if "right" in line:
pyautogui.click(button="right")
elif "left" in line:
pyautogui.click()
time.sleep(0.1) # 等待100毫秒
# 執行完畢
print("指令已執行完畢!")
settings.txt 使用者自行定義的動作檔案
moveTo 300 200
click right
moveTo 230 140
click left
click left
包成 .exe 執行檔(若目標執行環境為windows 7 請用 Python 3.8)
pip install pyinstaller
pyinstaller --onefile program.py

Leave a comment