Is it possible to minimize the console in python with the standard librairy (without extra module)?

余生长醉 提交于 2019-12-09 18:48:08

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/5245520/is-it-possible-to-minimize-the-console-in-python-with-the-standard-librairy-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!