python - 如何关闭命令窗口中闪烁的光标?

标签 python text-cursor command-window

我有一个 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/

相关文章:

python - 如何将 url 值传递给 Scrapy 爬取中的所有后续项目?

Python - 通过连接坐标查找正方形的数量

python - Django:数据库级别或代码级别的TextField(字符串)数据压缩

c# - 使用 System.Windows.Automation 的 DDE

visual-studio - Visual studio 命令窗口——更多命令的一个别名

python - 数据被插入到 MySQL 但不是永久的 - Python

Vim 滚动而不改变光标在屏幕上的位置

android - 如何在 android 中更改 EditText 气泡颜色(光标下)?

javascript - 在 Javascript contenteditable div 中插入文本

matlab - 如何将 MATLAB 命令行窗口的内容保存到文件中?