我有一个编辑文件的函数,它是用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()
目前没有回答
相关问题 更多 >
编程相关推荐