2011-01-03 14 views

risposta

0

Ho trovato che il widget Edit nel pacchetto urwid è sufficiente per le mie esigenze. Questo non è il widget Textpad, ma qualcosa di diverso. Il pacchetto urwid è in generale più bello, comunque. Tuttavia, non è ancora indolore. Il widget Edit consente di inserire testo, ma non di sovrascriverlo (attivato con la chiave Ins), ma non è un grosso problema.

6

trovato questo c'è qualche minuto

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')) 

Ho fatto anche una funzione per fare una casella di testo:

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 

Per usarlo basta fare:

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

Grazie per lo sforzo. Ho già provato la casella di testo. Ma il suo editing è troppo semplice. Non puoi nemmeno inserire il testo. Speravo di trovare qualcosa di più simile a nano, ma incorporabile in un'app. – Keith

+1

Ho dovuto cambiare le ultime due righe in: 'stdscr.addstr (1,1, text) stdscr.refresh()' per farlo funzionare. Dopo aver inserito il testo nel pad di testo, ho dovuto premere Ctrl-G per inviarlo. –

3

textpad.Textbox(win, insert_mode=True) fornisce di base inserire il supporto. Backspace deve essere aggiunto però.

1

Il codice iniziale non ha funzionato, ha deciso di eseguire un hack, funziona in modalità di inserimento e quindi quando si preme Ctrl-G viene visualizzato il testo nella posizione corretta.

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) 
Problemi correlati