4

I wrote a program that uses the console. Most of the time, the user must see the console informations. For a specific function from command line, I would like to run the script without the console rises. I just don't want see the window but it can be in the task bar. I know I can use extra modules (gui, win32,..) to do that but I would like to use the standard python librairy.

Is it possible to do that?

The program should run on Windows. (python 2.7)

I specify... I know I can use pythonw.exe too. The question then is how to launch the same script with python.exe sometimes and with pythonw.exe (from command line) for a specific function?

Thammas
  • 973
  • 2
  • 9
  • 14

3 Answers3

6

Found this question via google, so to answer the question, how to minimize (not completely hide) the console window on Windows when running a Python script:

# Python 3
import ctypes
ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 6 )

GetConsoleWindow() will return the window handle for the current console.
ShowWindow(hWnd, nCmdShow) will set the properties for the specific window. 6 is SW_MINIMIZE. Click on the link for other parameters.

user136036
  • 11,228
  • 6
  • 46
  • 46
1

on windows there are two python executables in your installation, one is "python.exe", which is the one you typically use. There is another called "pythonw.exe" which is for gui programs. It works just like python.exe, but does not display a console at all.

cprinos
  • 151
  • 2
  • sorry, I didn't specify that but I know I can use pythonw.exe. Then the question is how to launch the script with python.exe sometimes and with pythonw.exe for a specific function. I will edit my question... – Thammas Mar 09 '11 at 12:28
0

My first idea would be to have a .pyw-script for that specific task, that is launched in this case. Then only the original script's console pops up for a short time.

Freax
  • 91
  • 2
  • @Freax : Actually, I use already your solution but it makes a duplicate script. I would like only one script if it's possible. – Thammas Mar 09 '11 at 12:23
  • @Thammas: What's stopping you from taking things apart and making two scripts, one with `.py` and one with `.pyw`? Why not make two separate scripts? – S.Lott Mar 09 '11 at 15:05
  • @S.Lott : Like I say, I use already this method. I ask the question to see if it's possible to do it without 2 scripts. I don't know everything about Python and I just would like to know if it's possible or really not ? – Thammas Mar 09 '11 at 18:05
  • @Thammas: If you do it, and it works, what's the problem? What specific problem do you have? – S.Lott Mar 09 '11 at 18:52
  • @S.Lott : Sorry, but why should it be a problem? I just ask a specific question to know if it's possible or not. You can call that curiosity or a way to know better python... I will edit my question to be more specific. – Thammas Mar 09 '11 at 20:23
  • @Thammas: It is possible **and** you are already doing it. You have your own answer. – S.Lott Mar 09 '11 at 20:46
  • @S.Lott : Sorry again. I'm learning python and I respect your profile here but the question was to do it from one only script! Just by curiosity... – Thammas Mar 09 '11 at 21:34
  • @Thammas: You do have it working from only a script. I cannot understand what's wrong with your solution. It's already perfect. What more could you want? Please **update** the question to explain how your current solution is not perfect and a good example for others to follow. – S.Lott Mar 09 '11 at 21:36