用tkinter画圆
时间: 2024-05-29 14:07:17 浏览: 15
使用tkinter可以通过Canvas类来绘制图形,其中包括了绘制圆的方法。以下是一个简单的示例代码,可以在tkinter窗口中绘制一个半径为50的圆:
```
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=200, height=200)
canvas.pack()
x, y = 100, 100
r = 50
canvas.create_oval(x-r, y-r, x+r, y+r)
root.mainloop()
```
其中,`create_oval`方法用于绘制椭圆或者圆形,它的参数依次为椭圆或圆形的左上角和右下角的坐标。在这个例子中,我们设置圆心坐标为(100, 100),半径为50。
相关问题
tkinter如何画圆
要使用tkinter画圆,可以使用Canvas组件的create_oval()方法。例如,下面的代码将在画布上绘制一个半径为50像素的圆:
```
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=200, height=200)
canvas.pack()
canvas.create_oval(50, 50, 150, 150)
root.mainloop()
```
在create_oval()方法中,前两个参数表示圆的左上角点的x坐标和y坐标,后两个参数表示圆的右下角点的x坐标和y坐标。因此,要画一个半径为50像素的圆,可以将左上角坐标设置为(50, 50),右下角坐标设置为(150, 150)。
用tkinter画圆第一象限为绿色第二象限为红色第三象限为蓝色第四象限为黄色
以下是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的圆。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)