0

I have written a code in Python using Tkinter and I have a problem.

There is some code:

main.py:

import animation as a

def draw_animation():
    copyfile(biological_system_filepath, 'biological_system_parameters.tmp')
    a.create_animation()
    print('It is done.')
    os.remove('biological_system_parameters.tmp')

animation = Button(root, text='Animation', command=draw_animation, width=50)
animation.pack()

root.mainloop()

animation.py:

def create_animation():
    style.use('ggplot')
    sns.set_palette(sns.color_palette("hls", 2))

    fig = plt.figure(figsize=(16, 10))

    trajectory = np.loadtxt('trajectory.txt', delimiter=' ', unpack=True)
    energy = np.loadtxt('energy.txt', delimiter=' ', unpack=True)
    velocity = np.loadtxt('velocity.txt', delimiter=' ', unpack=True)

    ax1 = fig.add_subplot(2, 2, (1, 3), projection='3d')
    obj1, = ax1.plot(trajectory[0], trajectory[1], trajectory[2])
    biological_system = [[], [], [], []]
    biological_system[0], biological_system[1], biological_system[2], biological_system[3] = np.loadtxt(
        'biological_system_parameters.tmp', delimiter=' ', unpack=True)
    ax1.plot(biological_system[0] / 10, biological_system[1] / 10, biological_system[2] / 10, 'o', markersize=1)
    trajectory_xlim, trajectory_ylim, trajectory_zlim = adjust_limits3D('trajectory.txt')
    ax1.set_xlim(trajectory_xlim)
    ax1.set_ylim(trajectory_ylim)
    ax1.set_zlim(trajectory_zlim)
    ax1.set_xlabel('x[nm]')
    ax1.set_ylabel('y[nm]')
    ax1.set_zlabel('z[nm]')
    ax1.set_title('Trajektoria cząstki')

    ax2 = fig.add_subplot(2, 2, 2)
    obj2, = ax2.plot(energy[0], energy[1])
    energy_xlim, energy_ylim = adjust_limits('energy.txt')
    ax2.set_xlim(energy_xlim)
    ax2.set_ylim(energy_ylim)
    ax2.set_xlabel('t[ns]')
    ax2.set_ylabel('E[eV]')
    ax2.set_title('Wykres energii całkowitej od czasu')

    ax3 = fig.add_subplot(2, 2, 4)
    obj3, = ax3.plot(velocity[0], velocity[1])
    velocity_xlim, velocity_ylim = adjust_limits('velocity.txt')
    ax3.set_xlim(velocity_xlim)
    ax3.set_ylim(velocity_ylim)
    ax3.set_xlabel('t[ns]')
    ax3.set_ylabel('v[m/s]')
    ax3.set_title('Wykres prędkości cząstki od czasu')

    def init():
        obj1.set_data([], [])
        obj1.set_3d_properties([])

        obj2.set_data([], [])

        obj3.set_data([], [])
        return obj1, obj2, obj3,

    def animate(i):
        nonlocal STEP
        j = i * STEP
        if j % 1000 == 0:
            print(j)
        obj1.set_data(trajectory[0, :j], trajectory[1, :j])
        obj1.set_3d_properties(trajectory[2, :j])

        obj2.set_data(energy[0, :j], energy[1, :j])

        obj3.set_data(velocity[0, :j], velocity[1, :j])
        return obj1, obj2, obj3,

    STEP = 1000
    frames_num = math.floor(len(energy[0]) / STEP)
    ani = animation.FuncAnimation(fig, animate, init_func=init, frames=frames_num, blit=True)

    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=60, metadata=dict(artist='Me'))

    ani.save('animacja.mp4', writer=writer)
    plt.show()

And I don't understand why function a.create_animation() is called over and over and 'It is done.' isn't printed until I click red cross button closing the animation window. I know it is called over and over because I print 'j' value in animate() method.

Example output:

0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 11000 12000 13000 14000 15000 16000 17000 18000 19000 20000 21000 22000 23000 24000 25000 26000 27000 28000 29000 30000 31000 32000 33000 34000 35000 0 1000 2000 3000 4000 5000 It is done.

  • You need to share what your ```a``` object is. I have no idea where this method comes from or why it may hang. Clearly though your code stops executing. You assume the method is being called over and over, but it is most likely in its own loop that doesn't end until you close the window. You may have to find another way to create the desired animation. https://stackoverflow.com/questions/47030572/generating-animation-in-tkinter https://matplotlib.org/3.1.1/api/animation_api.html – ShayneLoyd Nov 12 '19 at 18:55
  • I'm sorry. I could be more specific. I edited a post. – Artur Owczarek Nov 12 '19 at 21:37
  • nothing in your example indicates that `a.create_animation()` There is missing code here that could be the issue at hand. The only time `a.create_animation()` is call is as a result of clicking your button however it could be called elsewhere in your code as it is obvious you have not included some things. Try writing up a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Mike - SMT Nov 12 '19 at 22:24
  • `plt.show()` makes a loop. – Artur Owczarek Nov 12 '19 at 23:13
  • Looking at the documentation you could try ```plt.show(block=False)``` other than that you would have to find a solution outside of using matplotlib. Let me know if it works for you please as I'm not sure if I understand the documentation properly. https://matplotlib.org/api/_as_gen/matplotlib.pyplot.show.html?highlight=show#matplotlib.pyplot.show – ShayneLoyd Nov 18 '19 at 14:57

0 Answers0