Canvas polygon coords() using list comprehension

Discussion in 'Python' started by Dave Harris, May 28, 2004.

  1. Dave Harris

    Dave Harris Guest

    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()?

    Thanks,
    Dave Harris

    # cantest.py - canvas test program
    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, outline2)

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


    Traceback (most recent call last):
    File "cantest.py", line 24, in ?
    app = App(root)
    File "cantest.py", line 20, in __init__
    c.coords(p2, outline2)
    File "C:\PYTHON23\Lib\lib-tk\Tkinter.py", line 2039, in coords
    self.tk.splitlist(
    _tkinter.TclError: bad screen distance "170)]"
    Dave Harris, May 28, 2004
    #1
    1. Advertising

  2. Dave Harris

    Peter Otten Guest

    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
    Peter Otten, May 28, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Albert Wiersch
    Replies:
    14
    Views:
    1,538
    Neredbojias
    Aug 19, 2005
  2. Askari
    Replies:
    2
    Views:
    696
    Askari
    Aug 30, 2004
  3. Matthias Vodel

    Change coords of a canvas.line item

    Matthias Vodel, Jan 5, 2007, in forum: Python
    Replies:
    3
    Views:
    298
    Wojciech =?ISO-8859-2?Q?Mu=B3a?=
    Jan 5, 2007
  4. Debajit Adhikary
    Replies:
    17
    Views:
    673
    Debajit Adhikary
    Oct 18, 2007
  5. Vedran Furac(
    Replies:
    4
    Views:
    317
    Marc 'BlackJack' Rintsch
    Dec 19, 2008
Loading...

Share This Page