1

I'm trying to create a GUI with tkinter that shows me an animation of an event I captured and when I click on a Button the next event should be shown. So far my code does exactly that but the problem I have is that when I click on the button to display the next event, the old event is not cleared from the canvas and thus an overlap of the two, or when I click more often numerous animations is shown.

My code looks something like that:

import matplotlib.pyplot as plt
from matplotlib import animation
import Tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

# Framenumber of Images that shall be animated
# shape(Frames) = (number_of_events, frames_per_event)
Frames = get_Frames(tdms_file)

# event that shall be analysed
k = 0

fig = plt.figure()
ax = fig.add_subplot(111)

root = tk.Tk()
root.title("My Animation GUI")

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
canvas.get_tk_widget().grid(row=0,column=1)

def event_animation(Frames, ax, k, canvas):

    ims = []

    for i in range(len(Frames[k])):

        Image = get_image(Frames[k], i)     # function that grabs the images from a different file 
        im =  ax.imshow(Image, cmap='gray', animated=True)
        ims.append([im])

    ani = animation.ArtistAnimation(fig, ims, interval=25, blit=True, repeat_delay=1000)
    canvas.show()

# show next event
def next_event():
    global k
    k += 1
    ani_show = event_animation(Frames, ax, k, canvas)

# show previous event
def prev_event():
    global k
    k -= 1
    ani_show = event_animation(Frames, ax, k, canvas)

prev_cell_button = tk.Button(root, text='<--', width=10, command = prev_cell)
prev_cell_button.grid(row=0, column=0)
next_cell_button = tk.Button(root, text='-->', width=10, command = next_cell)
next_cell_button.grid(row=0, column=2)

tk.mainloop()

I already tried clearing the axes with ax.cla() before creating a new animation and edited my animation function like in this example: stop / start / pause in python matplotlib animation

The function to create the animation will then look like this:

def event_Funcanimation(Frames, ax, k, canvas):

    Image = get_image(Frames[k], 0)
    im = ax.imshow(Image, cmap='gray', animated=True)

    def ani_iterator():
        i = 0
        while i < len(Frames[k]):
            if not stop:

                Image = get_image(Frames[k], i)
                i += 1
                yield Image


    def show_image(ani_iterator):
        Image = ani_iterator
        im.set_array(Image)
        return im,

    ani = animation.FuncAnimation(fig, show_image, ani_iterator, 
                                  blit=True, interval=25, repeat = True)
    canvas.draw()
    return ani

where the stop variable in the while loop will be changed when I click on one of the buttons. I know that this won't clear the animation from the axes but only stop it, but it doesn't work anyway for my code.

The problem right now is, that the animation keeps looping in the canvas and when a new animation is started they overlap. I think this issue could be solved pretty easily if there is a possibility clear the animation from the canvas before starting a new one. But I couldn't find any solution that helps me with my problem. And I want the animation loop, so setting repeat = False is not an option. Also this doesn't solve the issue, as the animations will still overlap, unless they are finished.

Thanks in advance for any help.

Community
  • 1
  • 1
Felix Reichel
  • 78
  • 2
  • 8
  • Is that `FuncAnimation` instance the animation object that is actually used in your code? It is a little bit unclear what is the actual code due to expressions like 'something like that' and 'will then look like this'. – J.J. Hakala Jul 14 '16 at 20:40
  • I tried both approaches but actually want to use the event_animation function that uses the ArtistAnimation. For both approaches I end up with the same problem. So the actual code I want to use is the one shown firts in the text. Sorry for the unclear formulation. – Felix Reichel Jul 18 '16 at 12:08

0 Answers0