question: parameters for create_polygon() method

Andrew Bennetts andrew-pythonlist at puzzling.org
Wed May 5 01:11:31 EDT 2004


On Wed, May 05, 2004 at 04:30:03AM +0000, SeeBelow at SeeBelow.Nut wrote:
> Andrew Bennetts wrote:
> > 
> > On Wed, May 05, 2004 at 02:11:28AM +0000, SeeBelow at SeeBelow.Nut wrote:
> > > Tkinter's canvas object has the method create_polygon().
> > >
> > > The docs say the parameters should be like:
> > > (x1, y1, x2, y2, x3, y3, .....)
> > >
> > > But it's often much better programming practice to have a list of
> > > tuples, each point being a tuple, like this:
> > > [(x1, y1), (x2, y2), (x3, y3),......]
> > >
> > > Is there a good way to manipulate my polygons in the latter form, but
> > > still use create_polygon() to draw them?
> > 
> > create_polygon(*list_of_points)
> > 
> > -Andrew.
> 
> Thanks Andrew, but I don't understand it.  What's that * thing?
> 
> Is there a * operator that converts a list of tuples into one big tuple?

Sorry, I misinterpreted the problem slightly; I just checked the docs for
create_polygon and realised it takes an argument that is a sequence of
co-ords, rather than a variable number of args that are the co-ords.

I also realised that I wasn't thinking clearly anyway.

You really just need a flatten function, e.g.:

def flatten(iterable):
    for elem in iterable:
        for subelem in elem:
            yield subelem

You can then use this like so:

create_polygon(tuple(flatten(list_of_points)))

(There are more complex flatten recipes out there, such as
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/118845, but this one
is sufficient for your needs).

-Andrew.





More information about the Python-list mailing list