如何移除 Canvas 小部件周围的浅灰色边框? [英] How do I remove the light grey border around my Canvas widget?
问题描述
我一直在使用 Tkinter Canvas
小部件以查看是否可以制作一些美观的小部件,我有几个问题.
I've been messing with the Tkinter Canvas
widget in order to see if I could make some aesthetically pleasing widgets, and I have a few questions.
首先,为什么我的 Canvas 小部件周围有一个浅灰色边框,我该如何去掉它?
First, why is there a light grey border around my Canvas widget, and how do I get rid of it?
其次,为什么 Canvas 中最左上角的位置 (2,2)?看起来应该是 (0,0).
Secondly, why is the top left most position in the Canvas (2,2)? It seems like it should be (0,0).
我当前的脚本:
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()
推荐答案
第 6.8 节 为什么画布似乎不是从 0,0 开始? Tk 使用常见问题解答 描述了这种现象.
我能够通过对发布的源进行轻微更改来消除边框伪影...
I was able to eliminate the border artefact with slight changes to the posted source...
改变这个:
w = Canvas(master, width=150, height=40, bd=0, relief='ridge')
w.pack()
到:
w = Canvas(master, width=150, height=40, bd=0, highlightthickness=0, relief='ridge')
w.pack()
还有这个:
x0 = 2
y0 = 2
x1 = 151
y1 = 2
到:
x0 = 0
y0 = 0
x1 = 150
y1 = 0
有趣的是,"borderwidth"
属性没有任何区别,但我根据 FAQ 保留了它.
Interestingly enough, the "borderwidth"
attribute did not make a difference, but I left it in per the FAQ.
在 Canvas
初始化语句之后立即运行 w.config()
显示 highlightthickness
的默认值为 2> 和 0 表示边框宽度
.
Running w.config()
immediately after the Canvas
initialization statement showed the defaults to be 2 for highlightthickness
and 0 for border width
.
这篇关于如何移除 Canvas 小部件周围的浅灰色边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!