python - 不同的线宽与 canvas.create_line?

标签 python tkinter-canvas

有谁知道为什么我在下面的示例中 Canvas 上的线宽不同?

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

真的让我很沮丧,因为我不知道发生了什么。如果有人可以帮助我,那就太好了。谢谢!

最佳答案

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

关于python - 不同的线宽与 canvas.create_line?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2796306/

相关文章:

python - 使用用户 tkinter 输入作为 Matplotlib 的坐标绘制一条线

python - 如何使用 python 的 urllib2 库仅请求一次而不是两次身份验证 url

python - Python中有sqlite3.connect的独占创建模式吗? (仅当文件尚不存在时才创建)

python - 为什么分配给空列表(例如 [] = "")不会出错?

python - 从 Python 文件中删除反斜杠实例

python - 如何用 Python 绘制多边形?

Python Tkinter Canvas 获取线的基本方向

python - ImportError:python26.dll 的模块使用与此版本的 Python 冲突

python - 增加 tkSimpleDialog 窗口大小

python - 无法删除 Canvas 上的 tkinter 对象