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

问题描述 投票:1回答:2

我试图隐藏从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()
python windows python-3.5 windows-console
2个回答
2
投票

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

例如,

pyinstaller -F -w FILENAME

你可以通过执行来学习更多

pyinstaller -h

我希望这可以帮到你。


0
投票

从横向思考,解决方案可能是从Windows的Task Scheduler应用程序*运行您的应用程序,并将任务设置为Run whether user is logged on or not

qazxsw poi设置使应用程序无形地运行,这意味着屏幕上不会显示任何窗口,任务栏图标或控制台窗口。

*默认情况下,任务计划程序安装在Windows中。在Cortana中键入其名称以运行

© www.soinside.com 2019 - 2024. All rights reserved.