如何让Python等待函数结束?

时间:2015-04-10 10:13:05

标签: python

我在脚本中运行一系列功能(我自己定义):

generate(file)
cut(file)
norm(file)

所有这些功能都在操纵文件中的数据。例如:

import numpy as np
import matplotlib.pyplot as plt


def generate(file):
    data=np.genfromtxt(file+'.txt')
    np.save(file+'_coord',data)
    print len(data)


class cutGUI(object):
    def __init__(self,ax,data,file):
        self.file=file
        self.data=data
        self.ax = ax
        self.x0 = 0
        self.y0 = 0
        self.r = 0
        self.circ=plt.Circle((self.x0,self.y0),radius=self.r,color='red',
                         fill=False)
        self.ax.add_patch(self.circ)
        self.ax.figure.canvas.mpl_connect('button_press_event',self.on_press)
        self.ax.figure.canvas.mpl_connect('button_release_event',self.on_release)

    def on_press(self,event):
        if event.button==1:
            self.x0 = event.xdata
            self.y0 = event.ydata

    def on_release(self,event):
        if event.button==1:
            self.r = np.sqrt((self.x0-event.xdata)**2+(self.y0-event.ydata)**2)
            self.circ.center=self.x0,self.y0
            self.circ.set_radius(self.r)
            self.ax.figure.canvas.draw()
        elif event.button==3:
            indizes=np.where((self.data[::1,0]-self.x0)**2+(self.data[::1,1]
                          -self.y0)**2>self.r**2)
            self.data=np.delete(self.data,indizes,axis=0)
            np.save(self.file+'_coord',self.data)
            plt.close('all')


def cut(file):
    data=np.load(file+'_coord.npy')
    x,y,z=data[::1,0],data[::1,1],data[::1,2]
    fig=plt.figure(figsize=(10,10))
    ax=fig.add_subplot(111,aspect='equal')
    ax.scatter(x,y,s=0.5,c=z,cmap='gray',marker='.')
    GUI=cutGUI(ax,data,file)
    plt.show()

关键是切割功能打开一个GUI,其中File中的数据是手动操作的。因此,脚本必须通过按鼠标右键等待用户完成。目前,该脚本没有等待。我该如何实现呢?

0 个答案:

没有答案
相关问题