为什么 tkinter 使用黑色填充颜色而不是我指定的颜色?

Why is tkinter using a fill colour of black instead of the colour I specify?

我刚开始学习 tkinter 通过绘制 Mandelbrot 集。

我将填充颜色指定为字符串 '#0074bf',但它呈现为 black。为什么 kwarg 在调用中被忽略 c.create_oval(x, y, x, y, fill = '#0074bf')?

from tkinter import *

# Some globals
SIZE = 4
WIDTH = 600
HEIGHT = 600
ratio = (WIDTH / SIZE, HEIGHT / SIZE)
zmin = -SIZE / 2 - (SIZE / 2) * 1j
zmax = SIZE / 2 + (SIZE / 2) * 1j
ESCAPE_RADIUS = 4
max_iterations = 256

# Create the window with Canvas
master = Tk()
c = Canvas(master, width = WIDTH, height = HEIGHT)
c.pack()

# Define a function to iterate; here, the classic Mandelbrot set function, z -> z^2 + c
f =  lambda z, c : z * z + c

def iterate(pixel):
    """
    Given a pixel (as a complex number x + iy) return the
    number of iterations it takes to escape,
    or the final count if it doesn't.
    """
    z0 = px_to_cx(pixel)
    z = z0
    num_iterations = 0
    while abs(z) < ESCAPE_RADIUS and num_iterations < max_iterations:
        z = f(z, z0)
        num_iterations += 1
    return num_iterations - 1

def px_to_cx(pixel):
    return (pixel.real - WIDTH / 2) / ratio[0] + ((pixel.imag - HEIGHT / 2) / ratio[1]) * 1j

for y in range(HEIGHT):
    for x in range(WIDTH):
        num = iterate(x + y * 1j)
        if  num < max_iterations / 2:
            # Here, the fill argument I supply seems to be ignored:
            c.create_oval(x, y, x, y, fill = '#0074bf')

mainloop()

您正在创建一个一像素宽一像素高的椭圆。您看到的是椭圆轮廓的颜色。只有一个像素不足以 space 来绘制轮廓和内部。

您可以将 outline 属性设置为与填充颜色相同的颜色,或者将轮廓宽度(width 属性)设置为零。

这是一个示例,显示了两个不同的 1 像素椭圆块。一个的默认轮廓宽度为 1,另一个将轮廓宽度明确设置为零。请注意,第一个您看到的是轮廓颜色,第二个您看到的是填充颜色。

import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=200, height=200, background="black")
canvas.pack(fill="both", expand=True)

for x in range(100):
    for y in range(100):
        canvas.create_oval(x, y, x, y, outline="red", fill="green")

for x in range(100, 200):
    for y in range(100, 200):
        canvas.create_oval(x, y, x, y, outline="red", fill="green", width=0)

root.mainloop()