如何让程序在继续之前等待鼠标单击
How to make a program wait for mouse click before proceeding
我正在开发一个程序,该程序应该在两点之间迭代以找到函数的根,给定一定的容差。我的计划是绘制函数,然后让用户通过单击绘图指定两个点。我的问题是,在指定两个点之前,我还没有找到 "paus" 程序的任何方法,它只是继续运行。例如,下面的代码给出了 IndexError: list index out of range when trying to print coords[0][0]
from matplotlib import pyplot as plt
def on_press(event):
print('you pressed', event.button, event.xdata, event.ydata)
global ix, iy
ix, iy = event.xdata, event.ydata
coords = coords.append([ix, iy])
if len(coords) >1:
fig.canvas.mpl_disconnect(cid)
def get_clicks(fig):
cid = fig.canvas.mpl_connect('button_press_event', on_press)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.show()
coords = []
get_clicks(fig)
print(coords[0])
我想做的是让代码在 "get_clicks(fig)" 处等待,直到完成两次点击,然后继续执行其余代码。
更新。
代码现在看起来像这样:
def bisec(a,b,tol):
print("Hi there!")
return a,b,mid
def on_press(event):
print('you pressed', event.button, event.xdata, event.ydata)
global ix, iy
ix, iy = event.xdata, event.ydata
if len(coords)<=1:
global coords
coords.append([ix, iy])
if len(coords) >1:
#fig.canvas.mpl_disconnect(cid)
print(coords)
bisec(coords[0][0], coords[1][0], 10)
def get_clicks(fig):
global cid
cid = fig.canvas.mpl_connect('button_press_event', on_press)
fig = plt.figure()
ax = fig.add_subplot(111)
coords = []
get_clicks(fig)
plt.show()
这一切的目的是在提供2个坐标时调用bisec,这似乎是目前的结果,出色!
plt.show
应该在最后一行之前。
坐标应声明为全局坐标。
和
坐标 = coords.append([ix, iy])
应该替换为
coords.append([ix, iy])
关于你的最后一个问题,是的,这是可能的。
大意是把代码"inside":
if len(coords) >1:
例如:
if len(coords) >1:
do_what_I_mean(coords)
如果你真的想写:
fig.canvas.mpl_disconnect(cid)
你应该小心确保:
1) cid为全局变量
(最简单的方法:不要让一个功能获得点击,只需将其内容写在脚本中即可)
2) 并在 on_press 函数中如此声明
我正在开发一个程序,该程序应该在两点之间迭代以找到函数的根,给定一定的容差。我的计划是绘制函数,然后让用户通过单击绘图指定两个点。我的问题是,在指定两个点之前,我还没有找到 "paus" 程序的任何方法,它只是继续运行。例如,下面的代码给出了 IndexError: list index out of range when trying to print coords[0][0]
from matplotlib import pyplot as plt
def on_press(event):
print('you pressed', event.button, event.xdata, event.ydata)
global ix, iy
ix, iy = event.xdata, event.ydata
coords = coords.append([ix, iy])
if len(coords) >1:
fig.canvas.mpl_disconnect(cid)
def get_clicks(fig):
cid = fig.canvas.mpl_connect('button_press_event', on_press)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.show()
coords = []
get_clicks(fig)
print(coords[0])
我想做的是让代码在 "get_clicks(fig)" 处等待,直到完成两次点击,然后继续执行其余代码。
更新。
代码现在看起来像这样:
def bisec(a,b,tol):
print("Hi there!")
return a,b,mid
def on_press(event):
print('you pressed', event.button, event.xdata, event.ydata)
global ix, iy
ix, iy = event.xdata, event.ydata
if len(coords)<=1:
global coords
coords.append([ix, iy])
if len(coords) >1:
#fig.canvas.mpl_disconnect(cid)
print(coords)
bisec(coords[0][0], coords[1][0], 10)
def get_clicks(fig):
global cid
cid = fig.canvas.mpl_connect('button_press_event', on_press)
fig = plt.figure()
ax = fig.add_subplot(111)
coords = []
get_clicks(fig)
plt.show()
这一切的目的是在提供2个坐标时调用bisec,这似乎是目前的结果,出色!
plt.show 应该在最后一行之前。
坐标应声明为全局坐标。 和 坐标 = coords.append([ix, iy]) 应该替换为 coords.append([ix, iy])
关于你的最后一个问题,是的,这是可能的。 大意是把代码"inside":
if len(coords) >1:
例如:
if len(coords) >1:
do_what_I_mean(coords)
如果你真的想写: fig.canvas.mpl_disconnect(cid) 你应该小心确保:
1) cid为全局变量 (最简单的方法:不要让一个功能获得点击,只需将其内容写在脚本中即可)
2) 并在 on_press 函数中如此声明