Canvas polygon coords() using list comprehension

Peter Otten __peter__ at web.de
Fri May 28 03:14:18 EDT 2004


Dave Harris wrote:

> I have written a reasonably clean way to perform coordinate
> transformations on a polygon, but Tkinter Canvas is not being particularly
> cooperative. The following program has been distilled down to a minimum
> and generates a traceback (see below) when it runs.
> 
> It appears that the create_polygon() method is more versatile than the
> coords() method.
> 
> Could someone can suggest a way to have the list comprehension not
> generate tuples, that would probably create a coordinate list acceptable
> to coords()?

You can explicitly flatten the list:

import Tkinter
from Tkinter import *

class App:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        c = Canvas(frame, width=300, height=300)
        c.pack()

        outline1 = [(100, 0), (200, 50), (150, 150)]
        p1 = c.create_polygon(outline1, fill='', outline='black')
        p2 = c.create_polygon(outline1, fill='', outline='red')

        outline2 = [(x+10, y+20) for (x, y) in outline1]
        c.coords(p2, Tkinter._flatten(outline2))

root = Tk()
app = App(root)
root.mainloop()
 
Peter




More information about the Python-list mailing list