Python - 如何在脚本运行时隐藏 Windows 命令提示符屏幕?

标签 python windows command-prompt py2exe

当我的 python 脚本运行时,如何从桌面上删除这个黑色的命令提示符屏幕?

我使用 python 2 exe 将 service.py 脚本制作成 exe。一切正常,但是当 .exe 运行时,我有一个我不想显示的固定命令提示符。

enter image description here

服务.py:

#!/usr/bin/env python
import os
import ctypes
from subprocess import Popen, PIPE

def Mbox(title, text, style):
  ctypes.windll.user32.MessageBoxA(0, text, title, style)

python_check = os.path.exists("C:/Python27/python.exe")
g_check = os.path.exists("C:/dev_appserver.py")

if python_check & g_check:
  p0 = Popen(['C:/Python27/python.exe', 'C:/dev_appserver.py', '--host', '0.0.0.0', '--port', '8080', 'C:/application'], stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=True)
  out, err = p0.communicate("n\n")

else:
  Mbox('Notice', 'Launching service failed please make sure C:\Python27\python.exe and C:/dev_appserver.py', 0)

设置.py:

from distutils.core import setup
import py2exe
setup(console=['service.py'])

最佳答案

我刚从这里看到这个:
https://github.com/PySimpleGUI/PySimpleGUI/issues/200#issuecomment-732483328

你可以安装这个库:pip install pywin32
然后,您可以在项目顶部添加这些行:

import win32gui, win32con

hide = win32gui.GetForegroundWindow()
win32gui.ShowWindow(hide, win32con.SW_HIDE)

关于Python - 如何在脚本运行时隐藏 Windows 命令提示符屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24799155/

相关文章:

python - Flaskdance 不生成 HTTPS uri

c++ - Mingw x64 Windows : plugin needed to handle lto object

powershell - 如何将 Powershell 变量值传递到 cmd 提示符

macos - Bash 提示换行问题

windows - 在“任务计划程序”中的“路径开始”中被忽略

batch-file - 使用命令提示符查找和删除文件夹

python - 在 SCons 中创建混合(值集)CPPDEFINES

python - 匹配除三个连续双引号之外的所有内容

Python 套接字 [WinError 10057]?

windows - 如何通过代码(在 cmd 中)打开屏幕保护程序(Windows 7)?