I want to be able to create a plot, press one button or another depending on what the plot shows, and then plot the following object. However, I am having some trouble wih it: it seems I can’t make it “wait” untill a button is pressed. Also, I am wondering if it would be possible to pass some parameters to the press_event, like a path to save something.
Here is the scheme of the program in case it helps. Thanks a lot in advance!
# event definition
def ontype(event):
if event.key == '1':
do stuff 1
plt.savefig(...)
plt.clf()
elif event.key == '2':
do stuff 2
plt.savefig(...)
plt.clf()
elif event.key == '3':
do stuff 3
plt.savefig(...)
plt.clf()
# main program
...stuff
create figure
plt.show()
plt.gcf().canvas.mpl_connect('key_press_event',ontype)
You must call
plt.gcf().canvas.mpl_connect('key_press_event',ontype)beforeplt.show(). In non-interactive mode, the execution waits atplt.show()until the plot-window is closed.Alternatively, replace in your sample
plt.show()toplt.ion(), which enables interactive mode. But it depends on your specific needs which solution you prefer.Edit
New example using Tkinter
The class takes as arguments:
* a callback to create each plot
* a list of lists of arguments for each plot to generate (might each be an empty list)
* a list of class-names. For each class, a button is created
* a callback that is called each time a classification has been performed
Any feedback would be appreciated.
*EDIT 2 *
For your comment, a slightly modified version. For every iteration of the loop, a new window is opened
This helps to fit into your own for-loop, but you still have to give a function to do the plotting after the GUI was created.