Edit text using Python and curses Text window widget?

Does anyone have a working example of using the curses.textpad.Textbox widget to edit existing text? This, of course, is in a Linux terminal (e.g. xterm).

+7
source share
4 answers

I found that the Edit widget in the urwid package is sufficient for my needs. This is not a Textpad widget, but something else. Anyway, the urwid package is generally nicer. Nevertheless, he is still not ill. The Edit widget allows you to insert text, but not rewrite (switch using the Ins key), but this is not very important.

0
source

Found that there are several minutes

 import curses import curses.textpad stdscr = curses.initscr() # don't echo key strokes on the screen curses.noecho() # read keystrokes instantly, without waiting for enter to ne pressed curses.cbreak() # enable keypad mode stdscr.keypad(1) stdscr.clear() stdscr.refresh() win = curses.newwin(5, 60, 5, 10) tb = curses.textpad.Textbox(win) text = tb.edit() curses.beep() win.addstr(4,1,text.encode('utf_8')) 

I also created a function to create a text field:

 def maketextbox(h,w,y,x,value="",deco=None,underlineChr=curses.ACS_HLINE,textColorpair=0,decoColorpair=0): nw = curses.newwin(h,w,y,x) txtbox = curses.textpad.Textbox(nw) if deco=="frame": screen.attron(decoColorpair) curses.textpad.rectangle(screen,y-1,x-1,y+h,x+w) screen.attroff(decoColorpair) elif deco=="underline": screen.hline(y+1,x,underlineChr,w,decoColorpair) nw.addstr(0,0,value,textColorpair) nw.attron(textColorpair) screen.refresh() return txtbox 

To use it, simply do:

 foo = maketextbox(1,40, 10,20,"foo",deco="underline",textColorpair=curses.color_pair(0),decoColorpair=curses.color_pair(1)) text = foo.edit() 
+6
source

textpad.Textbox(win, insert_mode=True) provides basic insert support. However, you need to add backspace.

+3
source

The source code did not work, I decided to crack it, it works in the insert mode, and then when you press Ctrl-G, put the text in the right position.

 import curses import curses.textpad def main(stdscr): stdscr.clear() stdscr.refresh() win = curses.newwin(5, 60, 5, 10) tb = curses.textpad.Textbox(win, insert_mode=True) text = tb.edit() curses.flash() win.clear() win.addstr(0, 0, text.encode('utf-8')) win.refresh() win.getch() curses.wrapper(main) 
+1
source

All Articles