等待用户单击Matplotlib图形中的按钮继续程序

2024-09-28 17:19:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个交互式程序,它把点放在Matplotlib图形上。该程序允许用户用鼠标将这些点从其原始位置移动。当用户对新的点位置满意时,程序继续。在

因为我的程序项目很大,而且涉及的范围远远不止这些,所以我把它分成几个文件。这些是:

  • 在函数.py在
import numpy as np
import settings

def update(val):

    settings.ct_plt.set_xdata(settings.x)
    settings.ct_plt.set_ydata(settings.y)

    settings.f_img.canvas.draw_idle()

def reset(event):

    settings.x = settings.x0.copy()
    settings.y = settings.y0.copy()

    settings.ct_plt.set_xdata(settings.x)
    settings.ct_plt.set_ydata(settings.y)

    settings.f_img.canvas.draw_idle()

def button_press_callback(event):
    'whenever a mouse button is pressed'

    if event.inaxes is None:
        return
    if event.button != 1:
        return

    settings.pind = get_ind_under_point(event)

def button_release_callback(event):
    'whenever a mouse button is released'

    if event.button != 1:
        return

    settings.pind = None

def get_ind_under_point(event):
    'get the index of the vertex under point if within epsilon tolerance'

    tinv = settings.ax_img.transData 

    xr = np.reshape(settings.x,(np.shape(settings.x)[0],1))
    yr = np.reshape(settings.y,(np.shape(settings.y)[0],1))
    xy_vals = np.append(xr,yr,1)
    xyt = tinv.transform(xy_vals)
    xt, yt = xyt[:, 0], xyt[:, 1]
    d = np.hypot(xt - event.x, yt - event.y)
    indseq, = np.nonzero(d == d.min())
    ind = indseq[0]

    if d[ind] >= settings.epsilon:
        ind = None

    return ind

def motion_notify_callback(event):
    'on mouse movement'

    if settings.pind is None:
        return
    if event.inaxes is None:
        return
    if event.button != 1:
        return

    settings.x[settings.pind] = event.xdata 
    settings.y[settings.pind] = event.ydata 

    settings.ct_plt.set_xdata(settings.x)
    settings.ct_plt.set_ydata(settings.y)

    settings.f_img.canvas.draw_idle()

def centhappy(event):

    settings.happy_pos = True
  • 在设置.py在
^{pr2}$
  • 在主.py在
^{3}$

我的问题来自程序的等待部分。它应该等待全局变量settings.happy_pos变成true。这就是我使用这个time.sleep(5)命令的原因。我也尝试过:

input("press enter if you are happy with your points")

但是,无论如何,图形甚至没有被创建,程序也没有响应。(程序没有这个等待/输入部分就可以工作);你知道如何解决这个问题吗?我应该用螺纹吗?在

PS:我给了here这个例子中我用的随机图像来回答我的问题。在


Tags: 程序noneeventreturnifsettingsisdef