Lucidnonsense
Lucidnonsense

Reputation: 1243

Python Curses for windows not overwriting strings

I'm using the package from: http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses and I'm making a screen object like so:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import curses
import time

class Screen(object):
    def __enter__(self):
        self.stdscr = curses.initscr()
        curses.cbreak()
        curses.noecho()
        self.stdscr.keypad(1)
        curses.curs_set(0)

        Y, X = self.stdscr.getmaxyx()

        gap = 1
        self.bar = self.stdscr.derwin(3, X, 1, 0)
        self.threads = self.stdscr.derwin(Y-4-gap, X/2, 4, 0)
        self.messages = self.stdscr.derwin(Y-4-gap, X/2 -1, 4, X/2 + 1)

        # self.bar.box()
        # self.threads.box()
        # self.messages.box()
        return self

    def __exit__(self, type, value, traceback):
        curses.nocbreak()
        self.stdscr.keypad(0)
        curses.echo()
        curses.endwin()

    def refresh(self):
        self.bar.refresh()
        self.threads.refresh()
        self.messages.refresh()



if __name__ == '__main__':
    with Screen() as S:
        S.bar.addstr(0, 0, "Hello")
        S.refresh()
        time.sleep(2)
        S.bar.addstr(0, 0, "\rHi")
        time.sleep(2)

But when I actually execute this it just stays as "Hello" rather than overwriting it and printing "hi"

I've tried removing the '\r' but to no avail.

Any ideas?

Upvotes: 1

Views: 542

Answers (1)

Huan-Yu Tseng
Huan-Yu Tseng

Reputation: 566

If you want to clean the "Hello", you can add S.bar.erase() before S.bar.addstr(0, 0, "\rHi"). And you also need to add S.refresh() after S.bar.addstr(0, 0, "\rHi") to update the window, or the "Hi" will not be displayed.

The window.erase() is used to clear the window. You can check the documentation for more details: https://docs.python.org/2/library/curses.html

Upvotes: 1

Related Questions