Different line widths with canvas.create_line?

Does anyone know why I am getting 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()

      

This really frustrates me as I have no idea what's going on. If anyone can help me it would be fantastic. Thanks!

+2


a source to share


2 answers


When you draw a line wider than 1, the extra pixels need to be drawn somewhere. As you will notice in your follow-up post, some of these pixels are coming out of the screen. All you have to do is adjust the origin coordinates to accommodate the line width.



0


a source


After some experimentation, I think I can see what's going on - part of the line on the left is being drawn outside of the canvas, which I think is really slowed down. Do I need to draw the line so that its outer bit is on the canvas? Alternatively, is there an easier way to draw a border around the widget or on the canvas?



0


a source







All Articles