我试图寻找一个类似的问题,有人可能但却找不到。长话短说。我整理了一个python脚本,它创建一个文件并写入并从中读取。然后我用nssm-2.24创建了.exe并将exe作为服务安装,它可以工作。
然后我创建了第二个脚本,它从python模块调用win32api和win32con,只需在屏幕上移动鼠标并执行一两次点击。这项工作来自python中,当编译为.exe。
时当我将第二个exe安装为服务时,它会显示为服务并运行,但鼠标不会在屏幕上移动。
Code below:
import autopy
import time
import win32api
import win32con
def click(x, y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
def move_mouse_around():
time.sleep(300)
autopy.mouse.smooth_move(500,500)
time.sleep(.`enter code here`5)
autopy.mouse.smooth_move(200,200)
time.sleep(1.5)
autopy.mouse.smooth_move(200,600)
time.sleep(1)
autopy.mouse.smooth_move(200,500)
time.sleep(1)
autopy.mouse.smooth_move(200,400)
time.sleep(1)
autopy.mouse.smooth_move(400,200)
time.sleep(.5)
autopy.mouse.smooth_move(400,300)
time.sleep(.5)
autopy.mouse.smooth_move(400,400)
time.sleep(.5)
autopy.mouse.smooth_move(400,450)
time.sleep(1)
autopy.mouse.smooth_move(50,50)
time.sleep(1)
click(50,50)
click(50,50)
time.sleep(.5)
autopy.mouse.smooth_move(50,150)
click(50,150)
autopy.mouse.smooth_move(500,500)
time.sleep(.5)
autopy.mouse.smooth_move(200,200)
time.sleep(1.5)
autopy.mouse.smooth_move(200,600)
time.sleep(1)
autopy.mouse.smooth_move(200,500)
time.sleep(1)
autopy.mouse.smooth_move(200,400)
time.sleep(1)
autopy.mouse.smooth_move(400,200)
time.sleep(.5)
autopy.mouse.smooth_move(400,300)
time.sleep(.5)
autopy.mouse.smooth_move(400,400)
time.sleep(.5)
autopy.mouse.smooth_move(400,450)
time.sleep(1)
autopy.mouse.smooth_move(17,50)
click(17,50)
click(17,50)
time.sleep(1.5)
def close_window():
autopy.mouse.smooth_move(1360,5)
click(1360,5)
time.sleep(30)
#close_window()
while True:
move_mouse_around()
答案 0 :(得分:2)
服务在非交互式会话(会话0)中执行,因此与用户的交互式桌面隔离。用户在会话1,会话2等中具有桌面。
所有这一切意味着您无法通过服务与用户的桌面进行交互。您必须在用户会话中,在与用户相同的桌面上执行此代码。