print at specific location on the screen in python

To print at a specific location on the screen in Python, we can use the curses module which provides an interface to control the terminal screen.

Here's a simple example:

main.py
import curses

# initialize curses
stdscr = curses.initscr()

# turn off echoing of keys
curses.noecho()

# turn on cbreak mode
curses.cbreak()

# enable keypad keys
stdscr.keypad(True)

# move cursor to row 5, column 10
stdscr.addstr(5, 10, "Hello World!")

# refresh screen
stdscr.refresh()

# wait for user input
stdscr.getch()

# turn off curses
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
419 chars
29 lines

In this example, we initialize curses by calling curses.initscr(). We then turn off echoing of keys using curses.noecho() and turn on cbreak mode using curses.cbreak(). We also enable keypad keys using stdscr.keypad(True).

We then move the cursor to row 5, column 10 using the stdscr.addstr(5, 10, "Hello World!") function. This function takes three arguments: the row, the column, and the string to print.

Finally, we refresh the screen using stdscr.refresh() and wait for user input using stdscr.getch(). We then turn off curses using curses.nocbreak(), stdscr.keypad(False), curses.echo(), and curses.endwin().

gistlibby LogSnag