Weird Tk Canvas coordinate issue

Tim Shannon shannon.timothy at gmail.com
Tue Apr 7 09:47:31 EDT 2009


Here's my actual code, but I've tried to strip out the irrelevant parts, but
it should compile.  FYI, I'm using 2.6 not 3.  The code is basically drawing
a grid with points in it.  I'm fairly new to Python, so constructive
criticism is appreciated.

class Editor:

    GSPACE = 80
    OFFSET = 50

    def __init__(self, master):

        #global variables
        self.grid = ex_Grid.Grid("", 10, 10)
        self.showPaths = IntVar()
        self.pathValid = False
        self.master = master
        self.isSaved = False

        master.title("ex_hack Editor")
        master.columnconfigure(0, weight=1)
        master.rowconfigure(0, weight=1)

        vscroll = Scrollbar(master)
        hscroll = Scrollbar(master)

        self.canvas = Canvas(master,
                             height=768,
                             width=1024,
                             yscrollcommand=vscroll.set,
                             xscrollcommand=hscroll.set)

        self.canvas.grid(column=0, row=0, sticky="wens")

        vscroll.config(command=self.canvas.yview)
        hscroll.config(command=self.canvas.xview,
                       orient="horizontal")

        vscroll.grid(column=1, row=0, stick="ns")
        hscroll.grid(column=0, row=1, sticky="we")
       #truncated for brevity


    def draw_grid(self):
        """Loads the current grid on the application canvas
            as well as into the other fields"""

        #print self.OFFSET
        self.canvas.delete(ALL)

        #draw Border
        self.canvas.create_rectangle(self.OFFSET,
                                     self.OFFSET,
                                     (self.grid.height * self.GSPACE) +
self.OFFSET,
                                     (self.grid.width * self.GSPACE) +
self.OFFSET,
                                     width=2)
        self.canvas.create_rectangle(self.OFFSET-4,
                                     self.OFFSET-4,
                                     (self.grid.height * self.GSPACE) +
self.OFFSET+4,
                                     (self.grid.width * self.GSPACE) +
self.OFFSET+4,
                                     width=2)
        #draw limit areas
        for l in self.grid.limitAreas:
            self.canvas.create_rectangle(self.g2c(l["x1"]) -
(self.GSPACE/4),
                                         self.g2c(l["y1"]) -
(self.GSPACE/4),
                                         self.g2c(l["x2"]) +
(self.GSPACE/4),
                                         self.g2c(l["y2"]) +
(self.GSPACE/4),
                                         outline="gray",
                                         fill="gray",
                                         stipple="gray12")


        #draw spaces
        for space in self.grid.grid.values():
            self.canvas.create_line(self.g2c(space.x),
                                    self.g2c(space.y),
                                    self.g2c(space.x)+1,
                                    self.g2c(space.y)+1,
                                    tags="space")
            if not space.is_empty():
                self.draw_space(space)


        self.canvas.config(scrollregion=self.canvas.bbox(ALL))



    def g2c(self, coord):
        """Converts grid locations to their actual coordinates on
            the drawing canvas"""

        return (coord * self.GSPACE) + self.OFFSET + (self.GSPACE / 2)






On Mon, Apr 6, 2009 at 5:11 PM, John Posner <jjposner at snet.net> wrote:

> Tim Shannon wrote:
>
>> I'm new to python, so keep that in mind.
>>  I have a tk Canvas that I'm trying to draw on, and I want to start my
>> drawing at an offset (from 0) location.  So I can tweak this as I code, I
>> set this offset as a class level variable:
>>  def ClassName:
>>    OFFSET = 20
>>
>>    def __init__(self, master)):
>>        self.canvas = Canvas(master)
>>        self.canvas.create_rectangle(self.OFFSET,
>>                                     self.OFFSET,
>>                                     300 + self.OFFSET,
>>                                     300 + self.OFFSET,
>>                                     width=2)
>>   The weird thing is, it doesn't offset.  If I set my offset to 100000, it
>> still starts drawing at 0,0. Here's the really weird part (at least to me),
>> if I put a print line right about my drawing statements to print the value
>> of the offset, it works like it should, and offsets the drawing.
>> If I remove the print line, the offset goes away.
>>  This makes no sense to me.
>>
> Tim Shannon wrote:
>
>> I'm new to python, so keep that in mind.
>>  I have a tk Canvas that I'm trying to draw on, and I want to start my
>> drawing at an offset (from 0) location.  So I can tweak this as I code, I
>> set this offset as a class level variable:
>>  def ClassName:
>>    OFFSET = 20
>>
>>    def __init__(self, master)):
>>        self.canvas = Canvas(master)
>>        self.canvas.create_rectangle(self.OFFSET,
>>                                     self.OFFSET,
>>                                     300 + self.OFFSET,
>>                                     300 + self.OFFSET,
>>                                     width=2)
>>
>>
> The above code wouldn't even compile. Please be careful to cut-and-paste
> working code into your email message. (I've made this mistake myself!)
> Changes to make:
>
> 1. first line: change "def" to "class"
>
> 2. def __init__(self, master)):    <--- get rid of extra ")"
>
> 3. Make sure to "pack" the canvas into the overall Tk window:
>
>   self.canvas = Canvas(master)
>   self.canvas.pack()                        <--- add this line
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090407/7d67d851/attachment-0001.html>


More information about the Python-list mailing list