我在MacOS上使用带Python3.7的Jupyter笔记本
我已经收集了很多数据,现在我想一次一个地将其绘制出来,并使用for循环遍历这些图形。但是,我想暂停查看每个图形,然后按任意键继续下一个图形
我尝试输入(“按ENTER键继续…”),但它会在每个图形后创建一个新行,并使整个Jupyter单元格输出在每次按ENTER键时向上滚动,因为它会为每个输入创建一个新行
所以我尝试使用curses库stdscr.getch()来捕获键盘输入,但是它没有检测到任何键盘按键
import curses
%matplotlib notebook
fig,ax = plt.subplots(figsize=(10, 8))
ch = 1
for col in df.columns:
if int(col) % 2 == 0: # even columns are time
xs = df[col]
else: # odd columns are voltage
if ch == 2:
ys = df[col]
ax.plot(xs, ys, 'b')
ax.set_xlabel('X') ; ax.set_ylabel('Y')
ax.set_xlim(-30,260) ; ax.set_ylim(-10000,10000)
fig.canvas.draw()
#input('Press ENTER to continue...')
stdscr = curses.initscr()
stdscr.keypad(True)
while True:
key = stdscr.getch()
if key != -1:
print(key)
if key == 27: # This is the escape key code
curses.endwin()
break
ax.cla()
ch = 1
else:
ch = 2
然而,在while-True循环中,key始终为-1。无论我按什么键,键总是-1,这意味着它根本检测不到任何键盘按键。我错过了什么?如何使用stdscr.getch()检测MacOS上Jupyter笔记本中的按键
谢谢大家!
任何Jupyter笔记本中的Python代码都在服务器内部运行,n/curses或任何其他类似的库将尝试获取服务器的键盘,或者Jupyter将禁用它。因此,ncurses将无法捕获按键
您最好的选择是@Tiago所说的:Jupyter小部件和事件处理程序。但是,这只适用于Jupyter,而不适用于在bash上运行的Python
https://minrk-ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html
从上面的站点:
据我所知,你无法在Jupyter笔记本中捕获键盘输入。Jupyter笔记本在没有键盘的服务器上运行
相关问题 更多 >
编程相关推荐