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中註冊的任何建議?