2

I've created a little Python app, and I want it to hide the console window in the middle of the process, so renaming it as .pyw won't solve the problem.

It would be best to have some kind of function to minimize the window, any thoughts?

Aeossa
  • 146
  • 1
  • 10

3 Answers3

2

On windows you may use win32api:

from win32 import win32api
from win32 import win32process
from win32 import win32gui

def callback(hwnd, pid):
  if win32process.GetWindowThreadProcessId(hwnd)[1] == pid:
    # hide window
    win32gui.ShowWindow(hwnd, 0)

# find hwnd of parent process, which is the cmd.exe window
win32gui.EnumWindows(callback, os.getppid())
Cubimon
  • 150
  • 3
  • 11
  • That's amazing, thanks for the reply. But I couldn't find anywhere where or how to actually install that api. Sorry for dumb question, I'm a beginner. – Aeossa Feb 28 '19 at 20:07
  • `pip install pywin32` in cmd – Cubimon Feb 28 '19 at 21:18
  • Yeah I already tried that, but then it throws errors like this `Cannot find reference 'win32gui/win32process/win32api' in 'imported module win32'`. Importing the whole module didn't solve it. – Aeossa Mar 01 '19 at 09:36
  • You may need to run `python Scripts/pywin32_postinstall.py -install`, see [github repo](https://github.com/mhammond/pywin32/blob/master/README.md). Or maybe `pip install pypiwin32`, see [this SO post](https://stackoverflow.com/questions/21343774/importerror-no-module-named-win32api#35948588). I don't know anymore how I installed it, I did it probably 1-2 years ago – Cubimon Mar 01 '19 at 12:32
  • Tried all of it, none helped :( – Aeossa Mar 01 '19 at 14:57
0

there is a good way to do it with cmd. open command prompt:

start /min py -x path\test.py

replace your version of python by 'x' and replace 'path' by the real path of your python project. this may help you to never see the console. you can start your program again in python like this:

import os
os.sysyem('start /min %~dp0test.py)

but I don't know the way to minimize the console in the middle of program.

ali
  • 1
  • 2
0

Much simpler method is to rename the main python source file's extension from py to pyw. This signals python that no console is required and it is a window'ed script.

E.g. rename PythonApp.py to PythonApp.pyw

Haven't tested but should work on all platforms.

Saqster
  • 119
  • 1
  • 1
  • 8