不同的线宽与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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您绘制宽度大于 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.
经过一些实验后,我想我明白了发生了什么 - 左边的一些线被画在画布之外,我认为这确实是迟缓的。有没有办法画一条线,使其最外面的部分在画布上?或者,是否有更简单的方法来在小部件周围或画布上绘制边框?
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?