简体   繁体   English

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

[英]Python - how can i hide the windows command prompt screen when the script is running?

How can i remove this black command prompt screen from the desktop when my python script is running?当我的 python 脚本运行时,如何从桌面上删除这个黑色的命令提示符屏幕?

I made the service.py script into exe using python 2 exe.我使用python 2 exe将service.py脚本制作成exe。 All worked fine but when the .exe is running i have a fixed command prompt which i do not want to show.一切正常,但是当 .exe 运行时,我有一个我不想显示的固定命令提示符。

在此处输入图片说明

service.py:服务.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:设置.py:

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

I just saw this from here:我刚从这里看到这个:
https://github.com/PySimpleGUI/PySimpleGUI/issues/200#issuecomment-732483328 https://github.com/PySimpleGUI/PySimpleGUI/issues/200#issuecomment-732483328

You can install this library: pip install pywin32你可以安装这个库: pip install pywin32
Then, you can add these lines at the top of your project:然后,您可以在项目的顶部添加这些行:

import win32gui, win32con

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

In the setup.py file you must write windows instead console .在 setup.py 文件中,您必须编写windows而不是console

Like this:像这样:

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

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

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