初级cnn研究辅助:python的matplotlib显示图片 之 按钮和触发事件

图片点击触发事件

点击左侧图片,显示右侧图片,并在你点击的位置画点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from matplotlib import pyplot as py
from matplotlib.widgets import Button,RadioButtons
import Image
def on_press(event):
if event.inaxes == None:
print "none"
return
fig = event.inaxes.figure
ax = fig.add_subplot(122)
img_gray = Image.open("./Alex.jpg").convert("L")
ax.imshow(img_gray, cmap="gray")
print event.x
ax1.scatter(event.xdata, event.ydata)
py.axis("off")
fig.canvas.draw()
if __name__ == "__main__":
img = Image.open("./Alex.jpg")
fig = py.figure()
fig.canvas.mpl_connect("button_press_event", on_press)
ax1 = fig.add_subplot(121)
ax1.imshow(img)
py.axis("off")
py.show()

效果:

解释:

1
2
3
4
5
fig.canvas.mpl_connect("button_press_event", on_press)#在这个figure上加点击事件,点击后的情况在自己写的on_press()方法里
def on_press(event):
event.inaxes.figure.canvas.draw()#用于图片刷新
event.x#事件的坐标用于其他按钮点击和figure点击发生冲突时判断返回
event.xdata,event.ydata#鼠标点击的位置,与上面那个坐标表示形式不同

普通按钮触发事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from matplotlib import pyplot as py
from matplotlib.widgets import Button,RadioButtons
import Image
def on_press(event):
if event.inaxes == None:
print "none"
return
fig = event.inaxes.figure
ax = fig.add_subplot(122)
img_gray = Image.open("./Alex.jpg").convert("L")
ax.imshow(img_gray, cmap="gray")
print event.x
ax1.scatter(event.xdata, event.ydata)
py.axis("off")
fig.canvas.draw()
def button_press(event):
print 'button is pressed!'
def draw_button():
global button#must global
point = py.axes([0.3,0.03,0.1,0.03])
button = Button(point, "click me")
button.on_clicked(button_press)
if __name__ == "__main__":
img = Image.open("./Alex.jpg")
fig = py.figure()
draw_button()
fig.canvas.mpl_connect("button_press_event", on_press)
ax1 = fig.add_subplot(121)
ax1.imshow(img)
py.axis("off")
py.show()

效果:

点击按钮,控制台输出“button is pressed!”

注解:

1
2
3
4
5
buttonax = plt.axes([0.8,0.03,0.05,0.03])#按钮的位置大小
button = Button(buttonax, "i'am a button")#按钮
button.on_clicked(save)#按钮的点击,save()是自己定议的点击后的事件
def save(event):
print 'i am press'

RadioButton

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def button_press(event):
print 'button is pressed!'
def radio_press(label):
print 'select: ',label
radiobutton.set_active(0)
def draw_button():
global button#must global
global radiobutton
print 'button'
point = py.axes([0.2,0.03,0.1,0.03])
button = Button(point, "click me")
button.on_clicked(button_press)
point_two = py.axes([0.6, 0.03, 0.2, 0.05])
radiobutton = RadioButtons(point_two, ("select me", "or me"))
radiobutton.on_clicked(radio_press)

if __name__ == "__main__":
img = Image.open("./Alex.jpg")
fig = py.figure()
draw_button()
fig.canvas.mpl_connect("button_press_event", on_press)
ax1 = fig.add_subplot(121)
ax1.imshow(img)
py.axis("off")
py.show()

效果:

注解:

1
2
3
radiobutton.set_active(0)#"DIFFERENT"被选择
def select(label):
print "label"#label is "DIFFERENT" or "SAME"

然后你可能会发现点击右侧按钮时也会触发右侧出现图片,这该怎么办呢?

在一的例子中有event.x就是干这个用的,通过判断这个值判断鼠标点击的位置。但是每人的电脑不同,结果不同。

文章作者:Lily

原始链接:/2018/04/08/%E5%88%9D%E7%BA%A7cnn%E7%A0%94%E7%A9%B6%E8%BE%85%E5%8A%A9%EF%BC%9Apython%E7%9A%84matplotlib%E6%98%BE%E7%A4%BA%E5%9B%BE%E7%89%87%20%E4%B9%8B%20%E6%8C%89%E9%92%AE%E5%92%8C%E8%A7%A6%E5%8F%91%E4%BA%8B%E4%BB%B6/

版权说明:转载请保留原文链接及作者。