How to disable the blinking cursor in the command window?

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

Any help would be greatly appreciated. Alan

+6
source share
3 answers

As far as you can tell, there is no Windows port for the curses module that you most likely need. What is closest to your needs is the Console Module , written by Fredrick Lund on effbot.org. Unfortunately, the module is only available for versions prior to Python 3, and this is what you seem 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)
+3
source

I wrote a color cross-platform library for use in conjunction with Colorama (http://pypi.python.org/pypi/colorama) for Python 3. To completely 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()

. , . , , Windows Vista Linux/Konsole.

+10

For those who see this in 2019, there is a Python3 module called a “cursor,” which basically just has hiding and showing methods. Set the cursor, then just use:

import cursor
cursor.hide()

And you did!

0
source

All Articles