1

matplotlib の図/プロットを使用して表示しようとしている一連の画像があります。nextまた、 を使用して作成したとpreviousボタンをクリックして、ユーザーが画像間を移動できるようにしたいと考えていますmatplotlib.widgets。これが私のコードです:

class Index(object):
   ind = 0

   def next(self, event):
      self.ind += 1
      orig_frame = cv2.imread(FFMPEG_PATH + all_frames[self.ind])
      ax.imshow(orig_frame)                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
      fig.canvas.draw()
      plt.waitforbuttonpress()


   def prev(self, event):
      self.ind -= 1
      if self.ind >= 0:
         orig_frame = cv2.imread(FFMPEG_PATH + all_frames[self.ind])
         ax.imshow(orig_frame)                                                                                                                                                                                                                                                                                                                                                                                   
         fig.canvas.draw()
         plt.waitforbuttonpress()

fig, ax = plt.subplots()
callback = Index()                                                                                                                                                                                               
start_frame = 0
orig_frame = cv2.imread(FFMPEG_PATH + all_frames[start_frame])
ax.imshow(orig_frame)

axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
next_button = Button(axnext, 'Next')
next_button.on_clicked(callback.next)
prev_button = Button(axprev, 'Previous')
prev_button.on_clicked(callback.prev)


plt.show()
plt.waitforbuttonpress()

問題は、関数の最後に and を配置したにもかかわらず、ボタンをfig.canvas.draw()クリックするとすぐにプログラムが終了することです。何が問題なのですか?plt.waitforbuttonpress()nextprevnext

4

0 に答える 0