python - Function being called even though Matplotlib button hasn't been clicked -
i have program shows image (fig 1). when image clicked shows colour in image clicked in separate matplotlib window (fig 2). fig 2 has buttons call different functions when clicked.
my problem functions meant called in fig 2 being called when fig 1 clicked.
the code looks this:
def show_fig1(img): # plot image plt.figure(1) ax = plt.gca() fig = plt.gcf() implot = ax.imshow(img) # detect click on image cid = fig.canvas.mpl_connect('button_press_event', on_pixel_click) plt.show(block=true) # called when fig1 clicked def on_pixel_click(event): if event.xdata != none , event.ydata != none: # computation here gets image fig2 img = get_fig2_img() show_fig2(img, event) def show_fig2(img, event): plt.figure(2) plt.imshow(img) # specify coordinates of button ax = plt.axes([0.0, 0.0, 0.2, 0.1]) # add button button = button(ax, 'button') # detect click on button button.on_clicked(test()) plt.show(block=true) def test(): print "button clicked" so test() called instantly when on_pixel_click() called though theoretically should wait until button clicked because of button.on_clicked() command.
any help?
thanks in advance :)
on line:
button.on_clicked(test()) you telling python execute test function, rather passing reference it. remove brackets , should sort it:
button.on_clicked(test)
Comments
Post a Comment