通过 .py 脚本执行的 .exe 文件隐藏控制台窗口

GLH*_*LHF 0 python windows windows-console python-3.5

我试图隐藏从 EXE 文件弹出的控制台窗口。我正在从我自己的 EXE(通过 PyInstaller 冻结的 Python 脚本)运行这个 EXE。

我发现,每当我通过 IDLE 或 PyCharm 运行脚本时,我都可以隐藏控制台窗口,并且一切正常。但是,如果我将脚本转换为 EXE(使用pyinstaller --onefile),则它不起作用。

我尝试了几乎所有 Google 和 SO 对我对这个问题的搜索的响应,但我仍然不知道如果我将脚本转换为 EXE 文件并运行它,我该如何隐藏控制台窗口。

我试过的最后一个:

import subprocess
import win32gui
import time

proc = subprocess.Popen(["MyExe.exe"])
# lets wait a bit to app to start
time.sleep(3)

def enumWindowFunc(hwnd, windowList):
    """ win32gui.EnumWindows() callback """
    text = win32gui.GetWindowText(hwnd)
    className = win32gui.GetClassName(hwnd)
    #print hwnd, text, className
    if text.find("MyExe.exe") >= 0:
        windowList.append((hwnd, text, className))

myWindows = []
# enumerate thru all top windows and get windows which are ours
win32gui.EnumWindows(enumWindowFunc, myWindows)

# now hide my windows, we can actually check process info from GetWindowThreadProcessId
# http://msdn.microsoft.com/en-us/library/ms633522(VS.85).aspx
for hwnd, text, className in myWindows:
    win32gui.ShowWindow(hwnd, False)

# as our notepad is now hidden
# you will have to kill notepad in taskmanager to get past next line
proc.wait()
Run Code Online (Sandbox Code Playgroud)

SaG*_*Gwa 5

您可以在 Pyinstaller 中使用 -w 选项。

例如,

pyinstaller -F -w 文件名

您可以通过执行了解更多信息

pyinstaller -h

我希望这可以帮助你。