import curses # Get a C character def CCHAR(ch): if type(ch)==str: return ord(ch) elif type(ch)==int: return ch else: raise Exception("CCHAR: can't parse a non-char/non-int value.") # Alternate character set def ALTCHAR(ch): if type(ch)==str: return ord(ch) | A_ALTCHARSET elif type(ch)==int: return ch | A_ALTCHARSET else: raise Exception("ALTCHAR: can't parse a non-char/non-int value.") def create_newwin(height, width, starty, startx): local_win = curses.newwin(height, width, starty, startx) local_win.box(0, 0) local_win.refresh() return local_win def destroy_win(local_win): local_win.border(CCHAR(' '), CCHAR(' '), CCHAR(' '), CCHAR(' '), CCHAR(' '), CCHAR(' '), CCHAR(' '), CCHAR(' ')) local_win.refresh() local_win.erase() ''' stdscr = curses.initscr() curses.cbreak() curses.noecho() curses.curs_set(0) stdscr.keypad(True) ''' def main(stdscr): height = 3 width = 10 LINES, COLS = stdscr.getmaxyx() starty = int((LINES - height) / 2) startx = int((COLS - width) / 2) stdscr.addstr("Use cursor keys to move the window, press Q to exit") stdscr.refresh() my_win = create_newwin(height, width, starty, startx) ch = 0 while ( (ch != CCHAR('q')) and (ch != CCHAR('Q')) ): ch = stdscr.getch() if ch == curses.KEY_LEFT: if startx - 1 >= 0: destroy_win(my_win) startx -= 1 my_win = create_newwin(height, width, starty, startx) elif ch == curses.KEY_RIGHT: if startx + width < COLS: destroy_win(my_win) startx += 1 my_win = create_newwin(height, width, starty, startx) elif ch == curses.KEY_UP: if starty - 1 > 0: destroy_win(my_win) starty -= 1 my_win = create_newwin(height, width, starty, startx) elif ch == curses.KEY_DOWN: if starty + height < LINES: destroy_win(my_win) starty += 1 my_win = create_newwin(height, width, starty, startx) curses.endwin() if __name__ == '__main__': curses.wrapper(main)