简体   繁体   English

Exe Win32api的Python脚本不起作用

[英]Python Script to Exe Win32api not working

I tried looking for a similar problem that someone perhaps had but could not find. 我试图寻找一个可能有人遇到但找不到的类似问题。 Long story short. 长话短说。 I put together a python script that creates a files and writes to it and reads from it. 我整理了一个python脚本,该脚本创建文件并写入文件并从中读取。 I then created the .exe with nssm-2.24 and installed the exe as a service and it works. 然后,我使用nssm-2.24创建了.exe并将该exe作为服务安装,并且可以正常工作。

I then created a second script that invokes the win32api and win32con from the python modules and just moves the mouse around on the screen and performs a click or two. 然后,我创建了第二个脚本,该脚本从python模块调用win32api和win32con,仅在屏幕上移动鼠标并单击一次或两次。 This work from within python and when compiled as .exe. 这项工作是在python中进行的,并在编译为.exe时使用。

When I install the second exe as a service it shows up as a service and runs but the mouse does not move across the screen. 当我将第二个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()

Services execute in a non-interactive session, session 0, and are therefore isolated from the user's interactive desktop. 服务在非交互式会话(会话0)中执行,因此与用户的交互式桌面隔离。 Users have desktops in session 1, session 2 etc. 用户在会话1,会话2等中拥有台式机。

What all of this means is that you cannot interact with a user's desktop from a service. 所有这些意味着您无法通过服务与用户的桌面进行交互。 You will have to execute this code in the user's session, on the same desktop as the user. 您将必须在与用户相同的桌面上的用户会话中执行此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM