views:

167

answers:

2

Does anyone have any idea why I get different line widths on the canvas in the following example?

from Tkinter import *
bigBoxSize = 150

class cFrame(Frame):
    def __init__(self, master, cwidth=450, cheight=450):
        Frame.__init__(self, master, relief=RAISED, height=550, width=600, bg = "grey")
        self.canvasWidth = cwidth
        self.canvasHeight = cheight
        self.canvas = Canvas(self, bg="white", width=cwidth, height=cheight, border =0)
        self.drawGridLines()
        self.canvas.pack(side=TOP, pady=20, padx=20)

    def drawGridLines(self, linewidth = 10):
        self.canvas.create_line(0, 0, self.canvasWidth, 0, width= linewidth )
        self.canvas.create_line(0, 0, 0, self.canvasHeight, width= linewidth )

        self.canvas.create_line(0, self.canvasHeight, self.canvasWidth + 2, self.canvasHeight, width= linewidth )
        self.canvas.create_line(self.canvasWidth, self.canvasHeight, self.canvasWidth, 1, width= linewidth )

        self.canvas.create_line(0, bigBoxSize, self.canvasWidth, bigBoxSize, width= linewidth )
        self.canvas.create_line(0, bigBoxSize * 2, self.canvasWidth, bigBoxSize * 2, width= linewidth)


root = Tk()
C = cFrame(root)
C.pack()
root.mainloop()

It's really frustrating me as I have no idea what's happening. If anyone can help me out then that'd be fantastic. Thanks!

A: 

After some experimentation I think I see what's happening - some of the line on the left is being drawn outside the canvas which I think is really retarded. Is there anyway to draw the line so that the outer most bit of it is on the canvas? Alternatively, is there any easier way to draw a border around a widget or on the canvas?

Sam
What you call retarded is more often referred to as "deterministic". That is, the canvas is just consistently applying its rules to the coordinates you give it. It doesn't know (or care) that you are drawing on the left edge or the right edge. You give it a width > 1 so it has to draw those extra pixels somewhere.
Bryan Oakley
The easiest way to draw a border around a widget is to use the widgets own border options. Another choice is to embed the widget inside a frame, and give that frame a border.
Bryan Oakley
A: 

When you draw a line with a width greater than 1, the extra pixels have to be drawn somewhere. As you observed in your own followup post, some of those pixels are being drawn off screen. All you need to do is adjust your original coordinates to take into account the width of the line.

Bryan Oakley
Thanks Bryan, I ended up doing exactly this after playing around with it for a while. I also tried playing with the border options but couldn't find an option for relief that would give me a solid line around the outside so I ended up just making a rectangle from the top corner of the canvas to the bottom corner.
Sam