TKinter Canvas 'dash' 选项未按预期运行

TKinter Canvas 'dash' option is not behaving as expected

canvas.create_line(50, 500, 950, 500, dash=(5, 1), tags="splitDistance")

这将创建一行,如下所示:

XXXXX XXXXX XXXXX XXXXX

5 个破折号,1 个 space 然后重复,如 this 文档所述

但是当将 1 更改为任何值时,它将不会改变间隙大小。

dash=(5, 100):

XXXXX XXXXX XXXXX XXXXX

另一个奇怪的行为是第一个参数仅在可被 5 整除时才改变结果,例如:(5, 10, 15, 20...)

dash=(1, 1):

X X X X X X X X X X X X

dash=(4, 1):

X X X X X X X X X X X X

dash=(5, 1):

XXXXX XXXXX XXXXX XXXXX

这是程序和结果的图像:

不同的平台支持不同的破折号模式。您所看到的是 Windows 不支持与 X-based 系统相同的破折号模式。

这些示例来自 canonical tcl/tk documentation on the dash attribute(采用 tcl 语法,但转换为 tkinter 很简单):

-dash .     → -dash {2 4}
-dash -     → -dash {6 4}
-dash -.    → -dash {6 4 2 4}
-dash -..   → -dash {6 4 2 4 2 4}
-dash {. }  → -dash {2 8}
-dash ,     → -dash {4 4}

文档接着说:

On systems which support only a limited set of dash patterns, the dash pattern will be displayed as the closest dash pattern that is available. For example, on Windows only the first 4 of the above examples are available. The last 2 examples will be displayed identically to the first one.