且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Matplotlib按钮

更新时间:2023-11-30 16:05:22

关于第一点,为什么需要它?在这种情况下,您可以忽略事件吗?

About the first point, why do you need that? Can you just ignore the event in that case?

关于第二点,您可以使用 bnext.label.clipbox.get_points()提取按钮的坐标,并将其与鼠标事件的坐标进行比较,如示例中所示下方:

Regarding the second point, you can use bnext.label.clipbox.get_points() to extract the coordinates of the button, and compare them with the coordinates of the mouse event, like in the example below:

import matplotlib.pylab as plt
from matplotlib.widgets import Button


fig,ax = plt.subplots()
ax.plot([1,2,3],[10,20,30],'bo-')
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')

(xm,ym),(xM,yM)=bnext.label.clipbox.get_points()

def on_press(event):

    if xm<event.x<xM and ym<event.y<yM:
        print "Button clicked, do nothing. This triggered event is useless."
    else:
        print "canvas clicked and Button not clicked. Do something with the canvas."
    print event
def on_button_clicked(event):
    print "button clicked, do something triggered by the button."
    print event

bnext.on_clicked(on_button_clicked)
fig.canvas.mpl_connect('button_press_event', on_press)
plt.show()