canvas.create_line的线宽不同?

时间:2010-05-09 01:34:25

标签: python canvas tkinter

有没有人知道为什么我在下面的例子中在画布上获得不同的线宽?

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

真的令我感到沮丧,因为我不知道发生了什么。如果有人能帮助我,那就太棒了。谢谢!

2 个答案:

答案 0 :(得分:0)

经过一些实验后,我想我看到了发生了什么 - 左边的一些线条是在画布之外绘制的,我认为它确实是迟钝的。是否有画线以使其最外面的部分在画布上?或者,是否有更简单的方法在窗口小部件或画布上绘制边框?

答案 1 :(得分:0)

当您绘制宽度大于1的线条时,必须在某处绘制额外的像素。正如您在自己的后续帖子中观察到的那样,其中一些像素正在屏幕上绘制。您需要做的就是调整原始坐标以考虑线条的宽度。