我有一个 Python 脚本,它使用 print() 函数将输出发送到 DOS 命令窗口(我使用的是 Windows 7),但我想防止(或隐藏)光标在下一个可用输出位置闪烁.有谁知道我该怎么做?我查看了 DOS 命令列表,但找不到合适的命令。
如有任何帮助,我们将不胜感激。 艾伦
最佳答案
我一直在编写一个跨平台颜色库以与 colorama 结合使用对于 python3。要在 windows 或 linux 上完全隐藏光标:
import sys
import os
if os.name == 'nt':
import msvcrt
import ctypes
class _CursorInfo(ctypes.Structure):
_fields_ = [("size", ctypes.c_int),
("visible", ctypes.c_byte)]
def hide_cursor():
if os.name == 'nt':
ci = _CursorInfo()
handle = ctypes.windll.kernel32.GetStdHandle(-11)
ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(ci))
ci.visible = False
ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))
elif os.name == 'posix':
sys.stdout.write("\033[?25l")
sys.stdout.flush()
def show_cursor():
if os.name == 'nt':
ci = _CursorInfo()
handle = ctypes.windll.kernel32.GetStdHandle(-11)
ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(ci))
ci.visible = True
ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))
elif os.name == 'posix':
sys.stdout.write("\033[?25h")
sys.stdout.flush()
以上是选择性复制粘贴。从这里你应该几乎能够做你想做的事。假设我没有弄乱复制和粘贴,这是在 Windows Vista 和 Linux/Konsole 下测试的。
关于python - 如何关闭命令窗口中闪烁的光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5174810/