1

入力項目の定義済みの名前を挿入するにはどうすればよいですか?

私の努力:(情報:文字「_」はカーソルです)

def Edit_Item(stdscr, item_name)
    stdscr.addstr(1, 2, "Item Name:")
    r = stdscr.getstr(2, 16, 15)
    return r

Edit_Item(stdscr, 'Foo')
Edit_Item(stdscr, 'Bar')

結果:

Item Name: _
Item Name: _

望ましい結果:

Item Name: Foo_
Item Name: Bar_

ご協力ありがとうございました。

4

3 に答える 3

0

私は通常これを行います:

import curses
screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
screen.keypad( 1 )
screen.border( 0 )
curses.curs_set( 2 )
string = "Foo"
max_len = 40 #max len of string
screen.addstr( 3, 1, "Item Name: " )
screen.addstr( 3, 12, string )
screen.refresh()
position = len( string )
x = screen.getch( 3, 12 + position )
while x != 27:
    if (x <= 122 and x >= 97) or x == 32 or (x <= 90 and x >= 65):
        if position == len( string ):
            if len( string ) < max_len:
                string += chr( x )
                position = len( string )
        else:
            string = string[ 0:position ] + chr( x ) + string[ position + 1: ]
    if x == 263:
        if position == len( string ):
            string = string[ 0:len( string ) - 1 ]
            position = len( string )
        else:
            string = string[ 0:position -1 ] + string[ position: ]
            if position > 0:
                position -= 1
    if x == 330:
        if position == len( string ) - 1:
            string = string[ 0:len( string ) - 1 ]
        else:
            string = string[ 0:position +1] + string[ position + 2: ]
    if x == curses.KEY_RIGHT:
        if position < len( string ):
            position += 1
    if x == curses.KEY_LEFT:
        if position > 0:
            position -= 1  

    screen.erase()
    screen.border( 0 )
    screen.addstr( 3, 1, "Item Name: " )
    screen.addstr( 3, 12, string )
    x = screen.getch( 3, 12 + position )

curses.endwin()
exit()
于 2015-06-14T20:14:00.197 に答える