暂停python并等待Matplotlib事件

时间:2014-03-04 03:55:15

标签: python matplotlib

我希望在等待用户按下回车时暂停我的python脚本 - 而焦点在于matplotlib图。

我尝试了以下代码,但是当脚本进入wile循环时,脚本会绑定事件处理。这样用户就无法绘制矩形。

class DrawRectangle:

    def __init__(self, image):
        self.waiting_for_entry = True
        '''
        Event handling that lets user draw a rectangle on an image. 
        when the user is done drawing they press "enter" to disconnect the canvas
        '''

    def on_key_press(self, event):
        if event.key == 'enter':
            '''
            disconnect event handling
            '''
            self.waiting_for_entry = False

    def return_image_inside(self):
        '''
        Return the portion of the image inclosed in the rectangle
        '''

if __name__ == "__main__":
    image = import_some_image() # import an image
    image_plot = plt.imshow(image)
    selector = DrawRectangle(image_plot)

    while selector.waiting_for_entry:
        pass

    working_data = selector.return_image_inside()

    '''
    do stuff with selected image
    '''

暂停程序

print "press enter to continue:"
raw_input()

是否有效,但需要用户将焦点返回到终端屏幕,然后按Enter键。

有关如何在matplotlib中注册事件之前暂停脚本的任何建议吗?

1 个答案:

答案 0 :(得分:0)

我认为你必须实现一个简单的(单一窗口)GUI,并在其中包含一个matplotlib图形后端。

你可能很幸运:matplotlib examples提供了一个很好的例子。请注意,在运行示例时,如何捕获按键并将其打印到终端,还会将其转发到matplotlib.backend_bases.key_press_handler

相关问题