Python 如何移除 Canvas 小部件周围的浅灰色边框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4310489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I remove the light grey border around my Canvas widget?
提问by rectangletangle
I've been messing with the Tkinter Canvaswidget in order to see if I could make some aesthetically pleasing widgets, and I have a few questions.
我一直在弄乱 TkinterCanvas小部件,以查看是否可以制作一些美观的小部件,我有几个问题。
First, why is there a light grey borderaround my Canvas widget, and how do I get rid of it?
首先,为什么我的 Canvas 小部件周围有一个浅灰色边框,我该如何摆脱它?
Secondly, why is the top left most position in the Canvas (2,2)? It seems like it should be (0,0).
其次,为什么在 Canvas (2,2) 中最左上角的位置?看起来应该是(0,0)。
My current script:
我目前的脚本:
from Tkinter import *
master = Tk()
master.configure(bg='black')
master.wm_attributes("-topmost", 1)
w = Canvas(master, width=150, height=40, bd=0,relief='ridge',)
w.pack()
color = 100
x0 = 2
y0 = 2
x1 = 151
y1 = 2
while y0 < 20 :
r = color
g = color
b = color
rgb = r, g, b
Hex = '#%02x%02x%02x' % rgb
w.create_line(x0, y0, x1, y1,fill=str(Hex), width=1)
color = color - 2
y0 = y0 + 1
y1 = y1 + 1
color = 10
while y0 < 40 :
r = color
g = color
b = color
rgb = r, g, b
Hex = '#%02x%02x%02x' % rgb
w.create_line(x0, y0, x1, y1,fill=str(Hex), width=1)
color = color + 4
y0 = y0 + 1
y1 = y1 + 1
mainloop()
采纳答案by T.P.
Section 6.8 Why doesn't the canvas seem to start at 0,0?of the Tk Usage FAQdescribes the phenomenon.
第 6.8 节 为什么画布似乎不是从 0,0 开始?的Tk Usage FAQ描述了这种现象。
I was able to eliminate the border artefact with slight changes to the posted source...
我能够通过对发布的源进行轻微更改来消除边界伪影...
Change this:
改变这个:
w = Canvas(master, width=150, height=40, bd=0, relief='ridge')
w.pack()
to:
到:
w = Canvas(master, width=150, height=40, bd=0, highlightthickness=0, relief='ridge')
w.pack()
and this:
和这个:
x0 = 2
y0 = 2
x1 = 151
y1 = 2
to:
到:
x0 = 0
y0 = 0
x1 = 150
y1 = 0
Interestingly enough, the "borderwidth"attribute did not make a difference, but I left it in per the FAQ.
有趣的是,该"borderwidth"属性并没有什么不同,但我将其保留在FAQ 中。
Running w.config()immediately after the Canvasinitialization statement showed the defaults to be 2for highlightthicknessand 0for border width.
w.config()在Canvas初始化语句之后立即运行显示默认值为2forhighlightthickness和0for border width。
回答by Bryan Oakley
The short answer is, the Canvas has two components which affect the edges: the border (borderwidthattribute) and highlight ring (highlightthicknessattribute).
简短的回答是,Canvas 有两个影响边缘的组件:边框(borderwidth属性)和高光环(highlightthickness属性)。
If you have a border width of zero and a highlight thickness of zero, the canvas coordinates will begin at 0,0. Otherwise, these two components of the canvas infringe upon the coordinate space.
如果边框宽度为零且高光厚度为零,则画布坐标将从 0,0 开始。否则,画布的这两个组件会侵犯坐标空间。
What I most often do is set these attributes to zero. Then, if I actually want a border I'll put that canvas inside a frame and give the frame a border.
我最常做的是将这些属性设置为零。然后,如果我真的想要一个边框,我会把画布放在一个框架内,并给框架一个边框。

