不同的线宽与canvas.create_line?

发布于 2024-09-01 00:14:11 字数 1245 浏览 3 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何时共饮酒 2024-09-08 00:14:11

当您绘制宽度大于 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.

爱本泡沫多脆弱 2024-09-08 00:14:11

经过一些实验后,我想我明白了发生了什么 - 左边的一些线被画在画布之外,我认为这确实是迟缓的。有没有办法画一条线,使其最外面的部分在画布上?或者,是否有更简单的方法来在小部件周围或画布上绘制边框?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文