I want a user input and then stop the loop with a specific keyboard press.
from __future__ import print_function
import sys
import numpy as np
import matplotlib.pyplot as plt
def press(event):
print('press', event.key)
sys.stdout.flush()
if event.key == 'x':
visible = xl.get_visible()
xl.set_visible(not visible)
fig.canvas.draw()
if event.key == 'enter':
cnt.append(1)
cnt=[]
List=[]
fig, ax = plt.subplots()
fig.canvas.mpl_connect('key_press_event', press)
ax.plot(np.random.rand(12), np.random.rand(12), 'go')
xl = ax.set_xlabel('easy come, easy go')
ax.set_title('Press a key')
plt.show()
plt.waitforbuttonpress() #non specific key press!!!
result=cnt[0]+cnt[1]
I want to stop the loop, wait for the user to press enter and then continue the code using the cnt after. But if I do not put plt.waitforbuttonpress()
, the code runs and finishes, but if I put plt.waitforbuttonpress()
any keyboard or mouse press will run and end the entire code.