【问题标题】:Python - how can i hide the windows command prompt screen when the script is running?Python - 脚本运行时如何隐藏 Windows 命令提示符屏幕?
【发布时间】:2014-09-08 01:34:58
【问题描述】:

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

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

service.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)

setup.py:

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

【问题讨论】:

  • 我同意@Trimax 的回答,但我想推荐 pyinstaller,因为它更简单且支持更多。对于一个简单的文件,没有控制台.... pyinstaller service.py --noconsole --onefile
  • @dev247 pyinstaller 支持 Python 3.3+ 吗?
  • @Trimax No PyInstaller 和 py2exe 都只支持 Python 2.7。看到这个StackOverflow 条目,基本上你必须使用cx_freeze。
  • 另外你可以看到support listings here
  • 但是,我现在使用 py2exe 0.9.2 和 Python 3.4 并且工作正常。在 pypi 中:pypi.python.org/pypi/py2exe/0.9.2.0

标签: python windows command-prompt py2exe


【解决方案1】:

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

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

import win32gui, win32con

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

【讨论】:

    【解决方案2】:

    在 setup.py 文件中,您必须写 windows 而不是 console

    像这样:

    windows=["service.py"],
        options={"py2exe":
           {"optimize": 2,
              "bundle_files": 0,
              "ascii": 0}}
    

    【讨论】:

    • 当我这样做时:stackoverflow.com/a/24803525/285594,没有像你建议的那样将控制台更改为 Windows。也像 windows 一样工作。
    • 因为windows中的服务不需要命令控制台。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2015-09-13
    • 2018-05-10
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多