How to turn off blinking cursor in command window?

Question:

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

Asked By: Alan Harris-Reid

||

Answers:

As far as one can tell, there is no Windows port for the curses module, which is most likely what you need. The thing that comes closest to meeting your needs is the Console module written by Fredrik Lundh at effbot.org. Unfortunately, the module is available only for versions prior to Python 3, which is what you appear to be using.

In Python 2.6/WinXP, the following code opens a console window, makes the cursor invisible, prints ‘Hello, world!’ and then closes the console window after two seconds:

import Console
import time

c = Console.getconsole()
c.cursor(0)
print 'Hello, world!'
time.sleep(2)
Answered By: Adeel Zafar Soomro

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("33[?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("33[?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.

Answered By: James Spencer

To anyone who is seeing this in 2019, there is a Python3 module called “cursor” which basically just has hide and show methods. Install cursor, then just use:

import cursor
cursor.hide()

And you’re done!

Answered By: Schwaitz

I’m surprised nobody mentioned that before, but you actually don’t need any library to do that.

Just use print('33[?25l', end="") to hide the cursor.

You can show it back with print('33[?25h', end="").

It’s as easy as that 🙂

Answered By: gruvw