2014-03-04 62 views
2

我想暂停我的python脚本,而我等待用户按enter键 - 焦点位于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中注册的任何建议?

回答

0

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

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