微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 ncurses 文本板中输入时,字符会打印两次

如何解决在 ncurses 文本板中输入时,字符会打印两次

我正在使用 ncurses 在 python 中创建一个简单的用户名输入字段。我修改了文档中的代码,发现当我输入单个字符时,比如 w,它会在文本键盘显示两个相同的字符,而不是给我单个字符 ({{1} } 而不是 ww)。所以我以为我把编码搞砸了,然后对例子中的textpad进行了1对1的克隆,出现了同样的问题。

预期的结果如下(我每次输入一个字符都会向左移动光标得到它)

image1

我实际得到的结果如下:

image2

我尝试复制示例,重新安装 ncurses 和 Python,我尝试使用 allacrity 而不是我通常使用的,但它们都不起作用。

文件代码如下:

w

编辑:我找到了解决方案。实际问题是在登录函数中,import curses from curses.textpad import TextBox,rectangle screen = curses.initscr() def center_text(stdscr,text,offset): # Gets the height and lenght of the screen scr_rows,scr_cols = stdscr.getmaxyx() # Gets the center of the screen mid_row = int(scr_rows / 2) mid_col = int(scr_cols / 2) # half of the lenght of the text mes_len_half = int(len(text)+10 / 2) x_pos = mid_col - mes_len_half # prints the text in the middle stdscr.addstr(mid_row+offset,x_pos,text) stdscr.refresh() # returns some data for the login section return mid_row+offset,text def login(stdscr): unamerow,unamecol,text = center_text(screen,"Username: ",0) # Creates the rectangle around the area,and the window for input editBox = curses.newwin(1,10,unamerow,unamecol+len(text)) rectangle(stdscr,unamerow-1,unamecol-1,unamerow+1,unamecol+len(text)+10) stdscr.refresh() # manages the input window,and turns it into a textBox Box = TextBox(editBox) Box.edit() uname = Box.gather() return uname if __name__ == "__main__": uname = login(screen) curses.endwin() print(uname) 处于打开状态,因此文本垫打印了字符,回显也是如此。所以我只是在函数的开头添加curses.echo()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。