通过 .py 脚本执行的 .exe 文件隐藏控制台窗口 [英] Hide console window by an .exe file executed by a .py script

查看:55
本文介绍了通过 .py 脚本执行的 .exe 文件隐藏控制台窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to hide a console window that pops up from an EXE file. I am running this EXE from my own EXE (a Python script frozen via PyInstaller).

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

I found out that, whenever I run the script via IDLE or PyCharm, I can hide console windows, and everything works fine. But if I turn my script into an EXE (using pyinstaller --onefile), then it's not working.

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

I tried almost every Google and SO response to my searches about this problem, and still I don't know how can I hide console windows if I turn my script into an EXE file and run it.

我尝试的最后一个:

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()

推荐答案

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

you can use -w option in Pyinstaller.

例如

pyinstaller -F -w 文件名

pyinstaller -F -w FILENAME

你可以通过执行了解更多

you can learn more by excute

pyinstaller -h

pyinstaller -h

希望能帮到你.

这篇关于通过 .py 脚本执行的 .exe 文件隐藏控制台窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆