Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off blinking cursor in command window?

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

like image 628
Alan Harris-Reid Avatar asked Mar 02 '11 23:03

Alan Harris-Reid


Video Answer


2 Answers

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.

like image 54
James Spencer Avatar answered Sep 20 '22 14:09

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!

like image 22
Schwaitz Avatar answered Sep 23 '22 14:09

Schwaitz