Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Canvas polygon coords() using list comprehension

Reply
Thread Tools

Canvas polygon coords() using list comprehension

 
 
Dave Harris
Guest
Posts: n/a
 
      05-28-2004
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)]"

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      05-28-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List comprehension in if clause of another list comprehension Vedran Furac( Python 4 12-19-2008 01:35 PM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
Canvas with scrollbars - how to get correct canvas coordinate when the scroll bars have moved? PhilC Python 2 10-25-2004 11:57 AM
Canvas scrolling - scrollBar become "disabled" on change in canvas Askari Python 2 08-30-2004 02:56 PM
Drawing a polygon using doubles Rick Java 3 10-08-2003 01:39 AM



Advertisments