How to turn off blinking cursor in command window?
Asked Answered
G

4

17

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

Gerardogeratology answered 2/3, 2011 at 23:25 Comment(0)
G
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)
Gyasi answered 3/3, 2011 at 3:1 Comment(2)
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.Gerardogeratology
Now, in the glorious year of 2019, there is a Windows port for curses: pypi.org/project/windows-cursesSelfish
I
33

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.

Involuted answered 4/5, 2012 at 20:47 Comment(4)
Nice solution! What do \033[?25h and \033[?25l do exactly? Looks magic.Mikelmikell
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).Mikelmikell
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).!Hornstein
To provide a failsafe, add the "cursor on" code to your promptAbri
Z
20

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!

Zodiac answered 26/1, 2019 at 18:53 Comment(2)
The cursor package is just a copy of James Spencer's code, with an added GPLv3 license (...?)Frambesia
Questionable though that is, the package even admits as much on their PyPI listing so it's probably not an attempt at malicious theft.Thanh
I
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 :)

Indigoid answered 1/1, 2022 at 18:5 Comment(7)
Love this solution, but will this work in windows?Moujik
@Moujik Yes, just tested it todayIndigoid
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.Beecham
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.Guest
This seems to be a unix only solution and if that's true, it should be mentioned in the response.Shaffert
It does seem to work for Windows, I'll try just in case!Anticlerical
Thanks, this works great in Windows Terminal! In my script, in order to make this code a little less cryptic, I made hide_cursor and show_cursor functions so that when the functions are called, it's a little more clear what's going on. def hide_cursor(): print('\033[?25l', end="") And: def show_cursor(): print('\033[?25h', end="")Simone
G
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)
Gyasi answered 3/3, 2011 at 3:1 Comment(2)
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.Gerardogeratology
Now, in the glorious year of 2019, there is a Windows port for curses: pypi.org/project/windows-cursesSelfish

© 2022 - 2024 — McMap. All rights reserved.