How to turn off blinking cursor in command window?

Alan Harris-Reid picture Alan Harris-Reid · Mar 3, 2011 · Viewed 16.1k times · Source

I have a Python script that sends output to a DOS command window (I am using Windows 7) using the print() function, but I would like to prevent (or hide) the cursor from blinking at the next available output position. Has anyone any idea how I can do this? I have looked at a list of DOS commands but I cannot find anything suitable.

Any help would be appreciated. Alan

Answer

James Spencer picture James Spencer · May 4, 2012

I've been writing a cross platform colour library to use in conjunction with colorama for python3. To totally hide the cursor on windows or 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()

The above is a selective copy & paste. From here you should pretty much be able to do what you want. Assuming I didn't mess up the copy and paste this was tested under Windows Vista and Linux / Konsole.