如何在命令窗口中关闭闪烁光标?

Ala*_*eid 9 python cursor command-window

我有一个Python脚本,使用print()函数将输出发送到DOS命令窗口(我使用的是Windows 7),但我希望防止(或隐藏)光标在下一个可用输出位置闪烁.有谁知道我怎么能这样做?我查看了DOS命令列表,但找不到合适的东西.

任何帮助,将不胜感激.艾伦

gru*_*uvw 17

我很惊讶之前没有人提到这一点,但实际上你不需要任何库来做到这一点。

只是用来print('\033[?25l', end="")隐藏光标。

您可以使用 来显示它print('\033[?25h', end="")

就这么简单:)

  • @Cyber​​Srikanth 是的,今天刚刚测试过 (2认同)

小智 16

我一直在编写一个跨平台颜色库,与Python 3的Colorama(http://pypi.python.org/pypi/colorama)结合使用.完全隐藏光标在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()
Run Code Online (Sandbox Code Playgroud)

以上是选择性复制和粘贴.从这里你几乎可以做你想做的事.假设我没有搞砸副本和粘贴,这是在Windows Vista和Linux/Konsole下测试的.


Sch*_*itz 6

对于在2019年看到此内容的任何人,都有一个名为“ cursor”的Python3模块,该模块基本上只是具有hide和show方法。安装光标,然后使用:

import cursor
cursor.hide()
Run Code Online (Sandbox Code Playgroud)

大功告成!

  • 光标包只是 James Spencer 代码的副本,并添加了 GPLv3 许可证(...?) (8认同)

Ade*_*mro 3

据了解,curses 模块没有 Windows 端口,而这很可能正是您所需要的。最能满足您需求的是由 Fredrik Lundh 在 effbot.org 编写的Console 模块。不幸的是,该模块仅适用于您似乎正在使用的 Python 3 之前的版本。

在 Python 2.6/WinXP 中,以下代码打开控制台窗口,使光标不可见,并打印“Hello, world!” 然后两秒后关闭控制台窗口:

import Console
import time

c = Console.getconsole()
c.cursor(0)
print 'Hello, world!'
time.sleep(2)
Run Code Online (Sandbox Code Playgroud)