Should this display text onto the curses's screen?
I am trying to get a minimal working example of a curses display, so that I can determine whether or not curses is working.
import curses import curses.textpad stdscr = curses.initscr() stdscr.border() stdscr.addstr(10,10, "test") stdscr.refresh() c = stdscr.getch() curses.endwin()
Will this small example write "test" to the screen, and wait for user input?
See also questions close to this topic
Python Curses Refreshing Text With a Loop
I'm connecting via Linux Mint's Xfce terminal ssh (also tried ssh -t) to a Raspberry Pi running Rasperian Stretch. On the Pi I have a Python-Curses example which I modified trying to get the current time to update every few seconds "in place" over writing itself. But it prints the time once initially and then never prints an updated time. But if I manually press "Enter" key on my laptop the time updates, if I press "Enter" fast multiple times the "enters" stack-up and the time shows updates for as many times as I pressed enter. There's probably something maybe basic I don't understand about Curses and terminals or Python in this case. Appreciate it. Here's code:
import curses import traceback import datetime import time try: # -- Initialize -- stdscr = curses.initscr() # initialize curses screen curses.noecho() # turn off auto echoing of keypress on to screen curses.cbreak() # enter break mode where pressing Enter key # after keystroke is not required for it to register stdscr.keypad(1) # enable special Key values such as curses.KEY_LEFT etc # -- Perform an action with Screen -- stdscr.border(0) stdscr.addstr(5, 5, 'Hello from Curses!', curses.A_BOLD) stdscr.addstr(6, 5, 'Press q to close this screen', curses.A_NORMAL) while True: # stay in this loop till the user presses 'q' #stdscr.addstr(7, 5, 'zztop',curses.A_NORMAL) ti = str((datetime.datetime.now().time())) stdscr.addstr(8, 5, 'Time: '+ ti, curses.A_NORMAL) time.sleep(3) ch = stdscr.getch() if ch == ord('q'): break # -- End of user code -- except: traceback.print_exc() # print trace back log of the error finally: # --- Cleanup on exit --- stdscr.keypad(0) curses.echo() curses.nocbreak() curses.endwin()
_curses.error: addwstr() returned ERR on changing nlines to 1 on newwin method
The code is:
from curses import * from curses.panel import * def main(stdscr): start_color() curs_set(0) init_pair(1, COLOR_BLACK, COLOR_CYAN) posy = posx = 0 window = newwin(1, 1, posy, posx) panel = new_panel(window) window.addstr('*', color_pair(1)) update_panels() doupdate() while True: key = stdscr.getch() if key == ord('j'): posy+=1 elif key == ord('k'): posy-=1 elif key == ord('h'): posx-=1 elif key == ord('l'): posx+=1 elif key == ord('q'): endwin() break panel.move(posy,posx) update_panels() doupdate() if __name__ == '__main__': wrapper(main)
I am getting this error:
Traceback (most recent call last): File "test_1_height_error.py", line 34, in <module> wrapper(main) File "/usr/lib/python3.7/curses/__init__.py", line 94, in wrapper return func(stdscr, *args, **kwds) File "test_1_height_error.py", line 12, in main window.addstr('*', color_pair(1)) _curses.error: addwstr() returned ERR
However if I change line 10 from window = newwin(1, 1, posy, posx) to window = newwin(2, 1, posy, posx) i.e change the nlines args to greater than 1 then it works fine.
I really don't understand why I am getting this issue.
How to reinitialize a curses program after being uninitialized
I am writing a program that needs to open vim, or any other curses text editor. After the the user closes the program I would like to be able to return to the program so I can redraw the screen etc. The problem is that vim returns to a normal terminal mode. My program still seems to be running getch, but displays my prompt my cursor is now visible.