python3.7.3 tkinter canvas画圆角矩形形成代码
时间: 2023-08-03 12:37:50 浏览: 70
可以使用 `create_arc` 和 `create_polygon` 方法来画圆角矩形,具体实现方式如下:
```python
import tkinter as tk
def round_rectangle(canvas, x1, y1, x2, y2, radius=25, **kwargs):
points = [x1+radius, y1,
x1+radius, y1,
x2-radius, y1,
x2-radius, y1,
x2, y1,
x2, y1+radius,
x2, y1+radius,
x2, y2-radius,
x2, y2-radius,
x2, y2,
x2-radius, y2,
x2-radius, y2,
x1+radius, y2,
x1+radius, y2,
x1, y2,
x1, y2-radius,
x1, y2-radius,
x1, y1+radius,
x1, y1+radius,
x1, y1]
return canvas.create_polygon(points, **kwargs, smooth=True)
# 创建画布
root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()
# 画圆角矩形
round_rectangle(canvas, 50, 50, 250, 250, radius=50, fill="blue")
# 运行窗口
root.mainloop()
```
在这个例子中,`round_rectangle` 函数接收画布、左上角和右下角的坐标、圆角半径和其他关键字参数,返回一个圆角矩形的图形对象。在函数内部,计算出圆角矩形的各个点的坐标,并使用 `create_polygon` 方法在画布上画出这个图形。注意,这里的 `smooth` 参数设置为 `True`,可以让圆角矩形的边缘更加平滑。
相关推荐
![exe](https://img-home.csdnimg.cn/images/20210720083343.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)
![](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)