Rob*_*Rob 4 python curses python-2.7 python-3.x python-curses
我正在使用Python的curses模块。在stdscr中,每当我按Enter键时,诅咒就会移到同一行的第一列。我对此有几个问题。
如果我想在Enter键上执行某些操作(执行某些功能或执行某些操作),那么在“如果”条件下会发生什么?例如
if (condition which will determine if ENTER was pressed or not)
somecode
Run Code Online (Sandbox Code Playgroud)是什么原因呢?
您需要curses.noecho()在初始化过程中调用。
有办法将诅咒移到下一行吗?
stdscr.move(y,x)将移动到绝对位置。stdscr.getyx()会告诉您您当前的位置。
如果我想在Enter键上执行某些操作(执行某些功能或执行某些操作),那么在“如果”条件下会发生什么?例如
您以为可以调用getch()并将结果与进行比较KEY_ENTER。实际上,您需要检查更多的值。根据您的终端设置,使用的库以及月相,您可能需要检查换行符(aka \n,^ J,ASCII 10)或回车符(\r,^ M,ASCII 13)。
c = stdscr.getch()
if c == curses.KEY_ENTER or c == 10 or c == 13:
# I hit ENTER
Run Code Online (Sandbox Code Playgroud)
示例程序:
import curses
# Thanks, http://www.ipsum-generator.com
ipsum = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer
nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla
quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent
mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum
lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent
per conubia nostra, per inceptos himenaeos.'''
try:
# Standard startup. Probably don't need to change this
stdscr = curses.initscr()
curses.cbreak()
curses.noecho()
stdscr.keypad(1)
# Silly program to write to the screen,
# wait for either <ENTER> or <Q>.
# On <ENTER>, mess with the screen.
# On <Q>, exit.
stdscr.addstr(0, 0, ipsum)
stdscr.move(0, 0)
stdscr.refresh()
i = 0
j = 0
while 1:
c = stdscr.getch()
if c == ord('q'):
exit(0)
if c == curses.KEY_ENTER or c == 10 or c == 13:
i += 1
if i % 3 == 0:
stdscr.addstr(0, 0, ipsum.lower())
if i % 3 == 1:
stdscr.addstr(0, 0, ipsum.upper())
if i % 3 == 2:
stdscr.addstr(0, 0, ipsum)
stdscr.move(0, 0)
if c == curses.KEY_DOWN:
y, x = stdscr.getyx()
maxy, maxx = stdscr.getmaxyx()
stdscr.move((y+1) % maxy, x)
stdscr.refresh()
finally:
# Standard shutdown. Probably don't need to change this.
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
Run Code Online (Sandbox Code Playgroud)
参考:
| 归档时间: |
|
| 查看次数: |
5726 次 |
| 最近记录: |