Simen Russnes
Simen Russnes

Reputation: 2270

Using Python 3 curses in only a small part of the terminal

Is there a way curses can be initiated to only take up part of the screen, while allowing regular prints to go out above it?

So in this provided example, all the normal print statements would go to the regular terminal, and would persist after the program finishes.

import curses
from curses import wrapper
from time import sleep
import random


def main(stdscr):
    stdscr.nodelay(True)
    stdscr.clear()
    participants = 5
    positions = dict()
    for i in range(participants):
    positions[i] = 0
    completed = 0
    game_winner = -1
    while True:
    _c = stdscr.getch()
    round_winner = random.randint(0, participants-1)

    winner_position = positions[round_winner]
    if winner_position < 10:
        positions[round_winner] = winner_position + 1

    stdscr.clear()
    for i in range(participants):
        position = positions[i]
        stdscr.addstr(i, 0, str(i+1) + ": ")
        stdscr.addstr(i, position+3, 'x')
        if position == 10:
            if game_winner < 0:
                game_winner = i + 1
            print(str(i + 1) + " completed the race!")
            positions[round_winner] = position + 1
            completed += 1
    sleep(1)
    if completed >= len(positions):
        break

    print("The winner is " + str(game_winner) + "!")


wrapper(main)

Upvotes: 1

Views: 444

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54668

The question is

Is there a way curses can be initiated to only take up part of the screen, while allowing regular prints to go out above it?

actually no: you can tell curses to pretend that it is using a smaller screen, but in that case it will use the top of the screen (by setting the LINES environment variable) and/or the left part (by setting COLUMNS), but that won't prevent it from using erasures which clear the "unused" part of the screen.

Upvotes: 1

Related Questions