48

Canvas審美的に美しいウィジェットを作成できるかどうかを確認するために、Tkinter ウィジェットをいじっていますが、いくつか質問があります。

まず、 Canvas ウィジェットの周りに薄い灰色の境界線があるのはなぜですか?どうすればそれを取り除くことができますか?

次に、Canvas の左上の位置がなぜ (2,2) なのですか? (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()
4

3 に答える 3

77

Section 6.8 Why doesn't the canvas seem to start at 0,0? of the Tk Usage FAQ describes the phenomenon.

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.

Running w.config() immediately after the Canvas initialization statement showed the defaults to be 2 for highlightthickness and 0 for border width.

于 2010-11-30T06:31:37.187 に答える
17

簡単に言うと、キャンバスには、境界線 (borderwidth属性) とハイライト リング (highlightthickness属性) という、エッジに影響を与える 2 つのコンポーネントがあります。

境界線の幅が 0 で、ハイライトの太さが 0 の場合、キャンバスの座標は 0,0 から始まります。そうしないと、キャンバスのこれら 2 つのコンポーネントが座標空間を侵害します。

私がよく行うのは、これらの属性をゼロに設定することです。次に、実際に境界線が必要な場合は、そのキャンバスをフレーム内に配置して、フレームに境界線を付けます。

于 2010-11-30T11:58:34.827 に答える