12

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

0

4 Answers 4

9

Found this there is 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 made a function to make a textbox:

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 just do:

foo = maketextbox(1,40, 10,20,"foo",deco="underline",textColorpair=curses.color_pair(0),decoColorpair=curses.color_pair(1))
text = foo.edit()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the effort. I did try the Textbox already. But its editing is just too basic. You can't even insert text. I was hoping to find something more like nano, but embeddable in an app.
I had to change the last two lines to: stdscr.addstr(1,1,text) stdscr.refresh() to get it working. After entering text in the textpad I had to press Ctrl-G to submit it.
Also, it says that curses.ACS_HLINE is not defined
6

textpad.Textbox(win, insert_mode=True) provides basic insert support. Backspace needs to be added though.

Comments

3

The initial code didn't work, decided to have a hack at it, this works on insert mode and then when you press Ctrl-G display's the text at 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)

Comments

3

I found that the Edit widget in the urwid package is sufficient for my needs. This is not the Textpad widget, but something different. The urwid package is overall nicer, anyway. However, it's still not pain-free. The Edit widget does allow inserting text, but not overwriting (toggled with Ins key), but that's not a big deal.

1 Comment

The urwid package is overall nicer. :) is also my up to now experience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.