Python Curses Backspace检测在主窗口中起作用,但在子窗口中不起作用

时间:2018-08-19 22:34:05

标签: python python-3.x terminal curses python-curses

我编写了此函数,该函数需要用户输入指定长度的指定屏幕:

Mapping 1 to random val 47
  Testing 47 < 50 true
Mapping 1 to random val 57
    Mapping 57 to NSNumber
      Mapping 57 to String
Mapping 2 to random val 6
  Testing 6 < 50 true
Mapping 3 to random val 33
  Testing 33 < 50 true
Mapping 4 to random val 21
  Testing 21 < 50 true
Mapping 5 to random val 2
  Testing 2 < 50 true
Mapping 6 to random val 75
  Testing 75 < 50 false
Mapping 7 to random val 45
  Testing 45 < 50 true
Mapping 8 to random val 72
  Testing 72 < 50 false
Mapping 9 to random val 1
  Testing 1 < 50 true
Mapping 10 to random val 67
  Testing 67 < 50 false
Mapping 2 to random val 3
  Testing 3 < 50 true
Mapping 2 to random val 3
    Mapping 3 to NSNumber
      Mapping 3 to String
Mapping 3 to random val 6
  Testing 6 < 50 true
Mapping 3 to random val 86
    Mapping 86 to NSNumber
      Mapping 86 to String
Mapping 4 to random val 72
  Testing 72 < 50 false
Mapping 5 to random val 2
  Testing 2 < 50 true
Mapping 5 to random val 73
    Mapping 73 to NSNumber
      Mapping 73 to String
Mapping 6 to random val 31
  Testing 31 < 50 true
Mapping 6 to random val 53
    Mapping 53 to NSNumber
      Mapping 53 to String
Mapping 7 to random val 31
  Testing 31 < 50 true
Mapping 7 to random val 42
    Mapping 42 to NSNumber
      Mapping 42 to String
Mapping 8 to random val 41
  Testing 41 < 50 true
Mapping 8 to random val 23
    Mapping 23 to NSNumber
      Mapping 23 to String
Mapping 9 to random val 52
  Testing 52 < 50 false
Mapping 10 to random val 13
  Testing 13 < 50 true
Mapping 10 to random val 81
    Mapping 81 to NSNumber
      Mapping 81 to String
Mapping 1 to random val 84
  Testing 84 < 50 false
Mapping 2 to random val 1
  Testing 1 < 50 true
Mapping 2 to random val 82
    Mapping 82 to NSNumber
      Mapping 82 to String
Mapping 2 to random val 10
    Mapping 10 to NSNumber
      Mapping 10 to String
Mapping 3 to random val 58
  Testing 58 < 50 false
Mapping 4 to random val 98
  Testing 98 < 50 false
Mapping 5 to random val 12
  Testing 12 < 50 true
Mapping 5 to random val 9
    Mapping 9 to NSNumber
      Mapping 9 to String
Mapping 5 to random val 59
    Mapping 59 to NSNumber
      Mapping 59 to String
Mapping 6 to random val 49
  Testing 49 < 50 true
Mapping 6 to random val 29
    Mapping 29 to NSNumber
      Mapping 29 to String
Mapping 6 to random val 57
    Mapping 57 to NSNumber
      Mapping 57 to String
Mapping 7 to random val 20
  Testing 20 < 50 true
Mapping 7 to random val 87
    Mapping 87 to NSNumber
      Mapping 87 to String
Mapping 7 to random val 51
    Mapping 51 to NSNumber
      Mapping 51 to String
Mapping 8 to random val 2
  Testing 2 < 50 true
Mapping 8 to random val 93
    Mapping 93 to NSNumber
      Mapping 93 to String
Mapping 8 to random val 11
    Mapping 11 to NSNumber
      Mapping 11 to String
Mapping 9 to random val 18
  Testing 18 < 50 true
Mapping 9 to random val 47
    Mapping 47 to NSNumber
      Mapping 47 to String
Mapping 9 to random val 96
    Mapping 96 to NSNumber
      Mapping 96 to String
Mapping 10 to random val 10
  Testing 10 < 50 true
Mapping 10 to random val 44
    Mapping 44 to NSNumber
      Mapping 44 to String
Mapping 10 to random val 76
    Mapping 76 to NSNumber
      Mapping 76 to String

我正在使用具有主要功能的curses.wrapper来设置所有窗口。当我在包装器给定的主窗口中调用get_str时,该函数按预期工作。按下退格键可将最后一个字符移出屏幕,并将光标向后移动。但是,当我在主窗口的子窗口中调用它时,“ ^?”显示,并且不会触发if tmp =='KEY_BACKSPACE'语句。这是我设置子窗口的方式:`

def get_str(scr, max_len):

    curses.echo()
    curses.curs_set(2)

    usr_in = ''
    cur_len = 1;
    tmp = scr.getkey()   

    while(tmp != '\n' and cur_len < max_len):
        if tmp == 'KEY_BACKSPACE' and cur_len > 1:
            cur_len -= 1                
            usr_in = usr_in[:-1]
            curs_pos = scr.getyx()
            scr.addstr(curs_pos[0], curs_pos[1], " ")
            scr.move(curs_pos[0], curs_pos[1])
        else:
            usr_in += tmp
            cur_len += 1
        tmp = scr.getkey()

    if(cur_len == max_len):
        usr_in += tmp

    curses.noecho()
    curses.curs_set(0)

    return usr_in 

1 个答案:

答案 0 :(得分:1)

子窗口不继承keypad设置。创建窗口时,如果要读取发送“任何”命名的 city name region 0 Toronto North 1 San Jose West 2 Houston South 3 Dallas South 符号的键,则必须进行设置。