1

What I want do do is to ask for a number in the middle of the animation, for in the future be able to animate n circles and be able to change it in the middle of the animation. But when I try asking for an input I get this:

QCoreApplication::exec: The event loop is already running

and the next frame doesn't start until i have given it an input

How do I try getting an input and if not, continue the animation with the past value?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random 
  
fig, ax = plt.subplots()
ax.set_xlim(0,100)
ax.set_ylim(0,100)

alphas = [0.5,0.5]

circle = plt.Circle((5, 10), 10, color='b', fill=True, alpha = alphas[0])
circle2 = plt.Circle((5, 10), 5, color='r', fill=True, alpha = alphas[1])

circles = [circle, circle2]

def init():
    for i in circles:
        i.center = (50,50)
        ax.add_patch(i)
    return circles


def animation_frame(k):
    try:
        num = int(input())
        
    except:
        pass

    finally:
        for j in circles:
            x,y = j.center
            r = random.uniform(-5,5)
            r2 = random.uniform(-5,5)
            
            #stop circles from going out
            if (((x + j.radius + r) <= 100 ) & ((x - j.radius + r) >= 0 )):
                x += r  
            else:
                x -= r
            if (((y + j.radius + r2) <= 100 ) & ((y - j.radius + r2) >= 0 )):
                y += r2  
            else:
                y -= r2


            j.center = (x, y)
        return circles




animation = FuncAnimation(fig,animation_frame, init_func=init,frames=360,interval=20,blit=True)
plt.show()
Edu Galindo
  • 165
  • 1
  • 8
  • 1
    Hi, take a look [here](https://stackoverflow.com/questions/63754139/how-to-do-something-till-an-input-is-detected-in-python3/63754195#63754195), you can use the `pynput` as explained. Just create a thread to accept input with a timeout. – Carlo Zanocco Sep 05 '20 at 18:17

0 Answers0