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

3 python windows py2exe command-prompt

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

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

在此输入图像描述

服务.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)
Run Code Online (Sandbox Code Playgroud)

设置.py:

from distutils.core import setup
import py2exe
setup(console=['service.py'])
Run Code Online (Sandbox Code Playgroud)

小智 5

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

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

import win32gui, win32con

hide = win32gui.GetForegroundWindow()
win32gui.ShowWindow(hide, win32con.SW_HIDE)
Run Code Online (Sandbox Code Playgroud)