python - 在 Windows 上执行脚本时隐藏控制台

标签 python windows console

如何在执行脚本时隐藏控制台?我想创建一个单独的函数来执行此操作(也许通过应用 os/sys 中的一些方法,我不知道),所以我不需要像将脚本扩展名从 .py 更改为 .pyw

最佳答案

如果您想在执行期间隐藏窗口(并且您的脚本仅适用于 Windows),则 ctypes也许对你来说是一种可能性。

使用this答案你可以接受输入然后隐藏它:

import ctypes

a = input('Input value here:')


kernel32 = ctypes.WinDLL('kernel32')

user32 = ctypes.WinDLL('user32')

SW_HIDE = 0

hWnd = kernel32.GetConsoleWindow()
user32.ShowWindow(hWnd, SW_HIDE)

# Do stuff here

这是通过 C ShowWindow() 运行的。您可以获得更多(深入)信息from the Windows's own documentation

关于python - 在 Windows 上执行脚本时隐藏控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36294271/

相关文章:

python - 使用 NLTK 中的 shif-reduce 解析器以尽可能多种不同的方式解析句子?

python - 如何从帖子请求中拆分正文

c++ - 如何防止Windows显示 "Debug Error! abort() has been called"对话框?

windows - OS X 中的窗口移动和调整 API

c# - 在同一控制台中启动进程

python - 数据框根据数据框中的日期连接列

python - (Kivy Python) 实例后重置 ScreenManager 屏幕方向

node.js - Windows 域中的 Linux 服务器上使用 NodeJS 进行 SSO

无法使用 ReadConsoleInput 从控制台读取(!@#$...和大写字母)

c++ - 用于键盘异步输入的库