Generator functions and user interfaces

Terry Reedy tjreedy at udel.edu
Thu Sep 11 01:22:38 CEST 2008



psaffrey at googlemail.com wrote:
> I'm trying to implement an interactive graph visualisation tool using
> matplotlib.
> 
> I want to use a spring layout, where nodes repulse each other and
> edges act as springs to pull connected nodes together. Usually, this
> algorithm runs through a number of iterations of attraction/repulsion
> to allow the nodes to converge to suitable positions. However, rather
> than running all these iterations to lay out the graph and then
> rendering it, I want to animate the graph as it is "springing" into
> place, and then allow the user to drag nodes around and have the graph
> redraw on the fly.
> 
> My idea for doing this was to use a generator function, where I yield
> the position of the nodes after each iteration and then call draw() on
> the position yielded. Does this seem like a sensible approach? 

To me, no.  The reasons for using one do not apply here.  What you 
describe is much like a game with auto updates plus user intervention. 
I would write an update function and add it to the gui mainloop (if 
running: update()) along with the draw function to be tied into the gui 
redraw (those parts I know little about).  I would allow use of the 
spacebar instead of or in addition to a button to toggle 'running' on 
and off.  That is standard in games that allow users to stop the action.

tjr




More information about the Python-list mailing list