2014-03-04 64 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