python3.7.3 tkinter canvas画圆角矩形形成代码
时间: 2023-06-04 16:06:15 浏览: 133
以下是使用Tkinter库实现画圆角矩形的Python代码:
```python
import tkinter as tk
def round_rectangle(x1, y1, x2, y2, radius, **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=400, height=400, bg='white')
canvas.pack()
round_rectangle(50, 50, 350, 350, 20, fill='pink', outline='black')
root.mainloop()
```
这段代码定义了一个`round_rectangle()`函数,以左上角坐标`(x1, y1)`和右下角坐标`(x2, y2)`为基础绘制圆角矩形。使用`points`列表存储矩形的各个顶点,根据圆角半径计算出相应的曲线点,最终传入`canvas.create_polygon()`方法绘制出圆角矩形。
在这个例子中,我们以左上角为`(50, 50)`,右下角为`(350, 350)`,圆角半径为`20`的矩形作为演示。
运行代码,程序窗口中将会绘制出一个填充色为粉色、边框颜色为黑色的圆角矩形。
阅读全文