18

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

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Alan Harris-Reid
  • 2,811
  • 8
  • 33
  • 32

4 Answers4

32

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.

yoogottamk
  • 49
  • 1
  • 5
James Spencer
  • 331
  • 3
  • 3
  • 1
    Nice solution! What do `\033[?25h` and `\033[?25l` do exactly? Looks magic. – normanius Apr 01 '20 at 22:27
  • Also, how to make sure in case of an unexpected failure that the console will return to its original state? If the application crashes (so terribly that no python exception can be caught), the cursor will be gone until one calls `printf "\033[?25h"` (in bash). – normanius Apr 01 '20 at 22:32
  • Nice! This is what I call clever and knowledgeable programming: one that uses already existing (standard) material and does not require extra modules/packages (beyond the standard ones, as 'ctypes' is in this case).! – Apostolos Jun 03 '20 at 17:32
  • To provide a failsafe, add the "cursor on" code to your prompt – kindall Apr 29 '22 at 22:56
22

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!

Schwaitz
  • 473
  • 3
  • 12
17

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

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

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

It's as easy as that :)

gruvw
  • 725
  • 8
  • 14
  • 1
    Love this solution, but will this work in windows? – CyberSrikanth Jan 16 '22 at 01:48
  • 2
    @CyberSrikanth Yes, just tested it today – gruvw Mar 11 '22 at 09:14
  • 1
    It doesn't work on all the terminals. This is called an ANSI escape character which might not work on all the terminals available out there. Eg: a common use case where this fails is VSCode with Bash terminal in Windows. – Dibakash Jan 24 '23 at 12:43
  • `colorama` currently does **not** map this to any windows API calls. -- this answer is a derivative of previously given answers. this solution WAS mentioned before. – Christoph Rackwitz Mar 28 '23 at 19:11
3

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)
Adeel Zafar Soomro
  • 1,492
  • 9
  • 15
  • thanks for the reply. You are right - I am using Python 3.1, so it looks like I am stuck with the cursor for a while yet :-( Regards. – Alan Harris-Reid Mar 07 '11 at 13:26
  • 1
    Now, in the glorious year of 2019, there is a Windows port for curses: https://pypi.org/project/windows-curses/ – Ben Ogorek Sep 22 '19 at 14:31