是否可以使用标准库(没有额外的模块)最小化 python 中的控制台?

2022-01-11 00:00:00 python console minimize standard-library

问题描述

我编写了一个使用控制台的程序.大多数时候,用户必须看到控制台信息.对于命令行中的特定功能,我想在没有控制台升起的情况下运行脚本.我只是不想看到窗口,但它可以在任务栏中.我知道我可以使用额外的模块(gui、win32、..)来做到这一点,但我想使用标准的 python 库.

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.

有可能吗?

该程序应在 Windows 上运行.(python 2.7)

The program should run on Windows. (python 2.7)

我指定...我知道我也可以使用 pythonw.exe.那么问题是如何有时使用 python.exe 和 pythonw.exe(从命令行)为特定功能启动相同的脚本?

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?


解决方案

通过google找到这个问题,所以回答这个问题,如何在运行Python脚本时最小化(不完全隐藏)Windows上的控制台窗口:

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() 将返回当前控制台的窗口句柄.
ShowWindow(hWnd, nCmdShow) 将为特定窗口设置属性.6SW_MINIMIZE.其他参数点击链接.

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.

相关文章