# # Tk_create_round_rectangle.py

Oct 24th, 2020 (edited)
506
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1.  # Tk_create_round_rectangle.py
2.
3. from tkinter import *
4. from math import sin, cos
5.
6. master = Tk()
7.
8. c = Canvas(master, width=230, height=230)
9. c.pack()
10.
11. def create_round_rectangle(x1, y1, x2, y2, feather, res=5, color='black'):
12.     points = []
13.     # top side
14.     points += [x1 + feather, y1,
15.                x2 - feather, y1]
16.     # top right corner
17.     for i in range(res):
18.         points += [x2 - feather + sin(i/res*2) * feather,
19.                    y1 + feather - cos(i/res*2) * feather]
20.     # right side
21.     points += [x2, y1 + feather,
22.                x2, y2 - feather]
23.     # bottom right corner
24.     for i in range(res):
25.         points += [x2 - feather + cos(i/res*2) * feather,
26.                    y2 - feather + sin(i/res*2) * feather]
27.     # bottom side
28.     points += [x2 - feather, y2,
29.                x1 + feather, y2]
30.     # bottom left corner
31.     for i in range(res):
32.         points += [x1 + feather - sin(i/res*2) * feather,
33.                    y2 - feather + cos(i/res*2) * feather]
34.     # left side
35.     points += [x1, y2 - feather,
36.                x1, y1 + feather]
37.     # top left corner
38.     for i in range(res):
39.         points += [x1 + feather - cos(i/res*2) * feather,
40.                    y1 + feather - sin(i/res*2) * feather]
41.
42.     return c.create_polygon(points, fill=color)
43.
44. create_round_rectangle(10, 10, 200, 100, 15)
45. create_round_rectangle(100, 40, 220, 200, 50, 15, 'blue')
46.
47. mainloop()
RAW Paste Data