用tkinter画圆第一象限为绿色第二象限为红色第三象限为蓝色第四象限为黄色
时间: 2023-05-29 09:04:44 浏览: 81
以下是Python代码:
```python
import tkinter as tk
# 创建画布
canvas_width = 400
canvas_height = 400
canvas = tk.Canvas(width=canvas_width, height=canvas_height)
canvas.pack()
# 画圆
radius = 100
center_x = canvas_width / 2
center_y = canvas_height / 2
for x in range(int(center_x), canvas_width):
for y in range(int(center_y), canvas_height):
distance = ((x - center_x) ** 2 + (y - center_y) ** 2) ** 0.5
if distance <= radius:
if x >= center_x and y <= center_y:
canvas.create_oval(x, y, x+1, y+1, fill='green')
elif x < center_x and y <= center_y:
canvas.create_oval(x, y, x+1, y+1, fill='red')
elif x < center_x and y > center_y:
canvas.create_oval(x, y, x+1, y+1, fill='blue')
else:
canvas.create_oval(x, y, x+1, y+1, fill='yellow')
# 进入主循环
canvas.mainloop()
```
在这个例子中,我们使用了两个嵌套的循环来遍历画布中的每个像素。对于每个像素,我们计算它到圆心的距离,如果距离小于等于半径,我们就根据它所在的象限来给它染色。最后,我们使用`create_oval`方法在该像素位置上画一个直径为1的圆。