不同的线宽与canvas.create_line?
有谁知道为什么我在下面的示例中在画布上得到不同的线宽?
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()
这真的让我很沮丧,因为我不知道发生了什么。如果有人能帮助我那就太好了。谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

当您绘制宽度大于 1 的线条时,必须在某处绘制额外的像素。正如您在自己的后续文章中所观察到的,其中一些像素正在被绘制出屏幕。您所需要做的就是调整原始坐标以考虑线的宽度。
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.