也许我在这方面的解决方案会有所帮助,我制作了一个文本框,该文本框通过按 Enter 键终止,所以你可以用它做任何你想做的事情 n hv 有趣:-
def txtpnl(stdscr, y=10, xl=10, wl=20, HIDE_WORDS = False):
wl += xl + 2
s = ''
textpad.rectangle(stdscr, y, xl, y + 2, wl)
stdscr.addstr(y + 1, xl + 1, '')
cp = 0
while True:
textpad.rectangle(stdscr, y, xl, y + 2, wl)
stdscr.addstr(y + 1, xl + 1 + cp, '')
k = stdscr.getch()
if k == KEY_ENTER or k in [10, 13]:
break
elif k == KEY_UP or k == KEY_DOWN:
pass
elif k == KEY_BACKSPACE or k == 8:
if cp > 0: cp -= 1
stdscr.addstr(y + 1, xl + 1, " " * len(s))
s = s[:cp]+s[cp+1:]
if HIDE_WORDS:
stdscr.addstr(y + 1, xl + 1 + cp, "*"*len(s[cp:]))
stdscr.addstr(y + 1, xl + 1, "*"*len(s[:cp]))
else:
stdscr.addstr(y + 1, xl + 1 + cp, s[cp:])
stdscr.addstr(y + 1, xl + 1, s[:cp])
elif k == KEY_LEFT or k == 27:
if not cp:
pass
else:
cp -= 1
if HIDE_WORDS:
stdscr.addstr(y + 1, xl + 1 + cp, "*"*len(s[cp:]))
stdscr.addstr(y + 1, xl + 1, "*"*len(s[:cp]))
else:
stdscr.addstr(y + 1, xl + 1 + cp, s[cp:])
stdscr.addstr(y + 1, xl + 1, s[:cp])
elif k == KEY_RIGHT or k == 26:
if cp == len(s):
pass
else:
cp += 1
if HIDE_WORDS:
stdscr.addstr(y + 1, xl + 1 + cp, "*"*len(s[cp:]))
stdscr.addstr(y + 1, xl + 1, "*"*len(s[:cp]))
else:
stdscr.addstr(y + 1, xl + 1 + cp, s[cp:])
stdscr.addstr(y + 1, xl + 1, s[:cp])
elif k in [KEY_DC, 127]:
if HIDE_WORDS:
stdscr.addstr(y + 1, xl + 1 + cp, "*"*len(s[cp + 1:] + " "))
stdscr.addstr(y + 1, xl + 1, "*"*len(s[:cp]))
else:
stdscr.addstr(y + 1, xl + 1 + cp, s[cp + 1:] + " ")
stdscr.addstr(y + 1, xl + 1, s[:cp])
s = s[:cp] + s[cp + 1:]
else:
if len(s) < wl - xl - 2:
if cp == len(s):
s += str(chr(k))
if HIDE_WORDS:
stdscr.addstr(y + 1, xl + 1, "*"*len(s))
else:
stdscr.addstr(y + 1, xl + 1, s)
else:
s = s[:cp] + str(chr(k)) + s[cp:]
if HIDE_WORDS:
stdscr.addstr(y + 1, xl + 1 + len(s[:cp + 1]), "*"*len(s[cp + 1:]))
stdscr.addstr(y + 1, xl + 1, "*"*len(s[:cp + 1]))
else:
stdscr.addstr(y + 1, xl + 1 + len(s[:cp + 1]), s[cp + 1:])
stdscr.addstr(y + 1, xl + 1, s[:cp + 1])
cp += 1
return s