GLHF
GLHF

Reputation: 4035

Hide console window by an .exe file executed by a .py script

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

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.

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.

The last one I tried:

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

Upvotes: 0

Views: 8501

Answers (4)

Ohad Cohen
Ohad Cohen

Reputation: 6144

If you are using a spec file - add console=False to your EXE(...) command:

exe = EXE(pyz,
      ...
      console=False )

Upvotes: 1

Lxix69
Lxix69

Reputation: 63

Ok, i know im late.You can hide console window in pyinstaller by using --noconsole

Such as this:

python PyInstaller Filename.py --onefile --noconsole

This will hide console window and program will execute normally.

Upvotes: 2

WonderWorker
WonderWorker

Reputation: 9062

Thinking laterally, a solution might be to run your application from Windows' Task Scheduler application* and set the task to Run whether user is logged on or not.

The Run whether user is logged on or not setting causes the application to run invisibly, meaning no widnows, taskbar icons or console windows will be displayed on-screen.

* Task Scheduler is installed in Windows by default. Type its name into Cortana to run

Upvotes: 0

SaGwa
SaGwa

Reputation: 602

you can use -w option in Pyinstaller.

for example,

pyinstaller -F -w FILENAME

you can learn more by excute

pyinstaller -h

I hope this can help you.

Upvotes: 6

Related Questions