You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python光标控制脚本退出VSCode后停止运行,求解决方法

解决退出VSCode后Python鼠标控制脚本停止运行的问题

你的问题核心是VSCode作为脚本的启动进程,关闭时会终止其衍生的所有子进程,包括你运行的Python脚本,导致后续鼠标操作无法执行。同时你的脚本里还有一处语法错误需要修正,以下是具体解决方法:

一、先修正脚本中的语法错误

代码里的mouse.position(1000,100)是错误写法,position是属性而非方法,应该用赋值语句:

mouse.position = (1000, 100)

二、脱离VSCode环境运行脚本

方法1:系统终端直接运行

  1. 打开Windows命令提示符(CMD)/PowerShell,或Mac/Linux终端
  2. 切换到脚本所在目录,例如:
    cd C:\Users\YourName\Scripts
    
  3. 执行脚本:
    python your_script_name.py
    
    这种方式下脚本由系统终端启动,和VSCode完全独立,关闭VSCode不会影响脚本运行。

方法2:打包成可执行文件

将脚本打包成独立的可执行程序,无需依赖Python环境和VSCode:

  1. 安装打包工具pyinstaller:
    pip install pyinstaller
    
  2. 进入脚本目录,执行打包命令:
    pyinstaller --onefile your_script_name.py
    
  3. 打包完成后,在生成的dist文件夹中找到对应的.exe文件,双击即可运行,完全脱离VSCode。

三、优化鼠标操作稳定性(可选)

为避免因鼠标操作过快导致系统未响应,建议在每个操作之间添加短暂延迟:

from pynput.mouse import Button,Controller
import webbrowser
import time

mouse=Controller()

url = 'google.com'

chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

mouse.position = (1500, 55)
time.sleep(0.5)
mouse.press(Button.left)
mouse.release(Button.left)

time.sleep(0.5)
mouse.position = (1400, 270)
time.sleep(0.5)
mouse.press(Button.left)
mouse.release(Button.left)

time.sleep(0.5)
mouse.position = (1000, 100)
time.sleep(0.5)
mouse.press(Button.left)
mouse.release(Button.left)

#webbrowser.get(chrome_path).open(url)

内容的提问来源于stack exchange,提问作者Unknown Chaos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 16:33:29