我具有编辑文件的此功能,并使用curses.wrapper()对其进行调用。我正在尝试为数字键盘0添加一个选项,以切换一个选项。但是,每次我按数字键盘上的任何键时,程序都会立即关闭。我确实尝试了数字小键盘(0)和数字小键盘(1),但所做的只是使numbpad可用于输入数字。我确实认为它与许多if语句有关,因为我可以在较小的程序中使用它,但是我想确保。任何帮助将不胜感激!
import curses
import sys,os
def edit_existing(stdscr, Lines):
stdscr.clear()
stdscr.refresh()
curses.mousemask(1)
#stdscr.keypad(0)
#stdscr.nodelay(1)
curses.raw()
height, width = stdscr.getmaxyx()
curses.start_color()
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
key, curs_x, curs_y, mouse_x, mouse_y, mouse_state, line_num, ows = 0, 0, 0, 0, 0, 0, 0, 1
for line in Lines:
temp = Lines[line_num]
temp = temp.ljust(width)
stdscr.addstr(line_num, 0, temp)
Lines[line_num] = temp
stdscr.refresh()
line_num += 1
empty_line = ''.ljust(width)
while True:
height, width = stdscr.getmaxyx()
stdscr.addstr(height - 1, width - 8, " ", curses.color_pair(1))
stdscr.addstr(height - 1, width - ((len(str(key)) + 1 + 1)), str(key), curses.color_pair(1))
if (curs_y not in Lines):
Lines[curs_y] = empty_line
if (key == curses.KEY_RIGHT):
curs_x += 1
elif (key == curses.KEY_LEFT):
curs_x -= 1
elif (key == curses.KEY_DOWN):
curs_y += 1
elif (key == curses.KEY_UP):
curs_y -= 1
elif (key == 27):
break
elif (key == curses.KEY_MOUSE):
_, curs_x, curs_y, _, mouse_state = curses.getmouse()
elif (key == 32):
stdscr.insch(curs_y, curs_x, " ")
tempstring = Lines[curs_y]
tempstring = list(tempstring)
tempstring.insert(curs_x, " ")
Lines[curs_y] = "".join(tempstring)
curs_x += 1
elif (key == 10):
stdscr.insertln()
curs_x = 0
curs_y += 1
elif (key == curses.KEY_BACKSPACE):
curs_x -= 1
if (curs_x != -1):
stdscr.delch(curs_y, curs_x)
tempstring = Lines[curs_y]
tempstring = list(tempstring)
del tempstring[curs_x]
Lines[curs_y] = "".join(tempstring)
elif (key == 330):
stdscr.addstr(curs_y, curs_x, " ")
tempstring = Lines[curs_y]
tempstring = list(tempstring)
tempstring[curs_x] = " "
Lines[curs_y] = "".join(tempstring)
elif (key != 0):
if (ows == 1):
stdscr.addstr(curs_y, curs_x, chr(key))
tempstring = Lines[curs_y]
tempstring = list(tempstring)
tempstring[curs_x] = chr(key)
Lines[curs_y] = "".join(tempstring)
else:
stdscr.insch(curs_y, curs_x, chr(key))
tempstring = Lines[curs_y]
tempstring = list(tempstring)
tempstring.insert(curs_x, chr(key))
Lines[curs_y] = "".join(tempstring)
curs_x += 1
if (curs_x >= width):
curs_y += 1
curs_x = 0
elif (curs_x < 0):
curs_x = 0
if (curs_y > 0):
curs_y -= 1
if (curs_y >= height):
curs_y = height - 1
elif (curs_y < 0):
curs_y = 0
stdscr.move(curs_y, curs_x)
stdscr.refresh()
key = stdscr.getch()
if (key == 112):
stdscr.addstr(0, 0, "NUM0 PRESSED")
stdscr.refresh()
curses.endwin()
return Lines
stdscr.refresh()
curses.endwin()