好吧,我会努力解释得更好。我需要使用tkinter的Canvas创建一个圆角矩形。我也需要一种方法,我可以改变颜色,从thahforth边界与元组,填补这个圆角矩形内,并改变角半径,直到角落。我看到this post,我创建了一个圆角矩形,其中的线条和弧线几乎都符合要求,但我不知道如何填充这个圆角矩形。
我的代码:
def rounded_rect(canvas: tkinter.Canvas,
x: Optional[int] = 1,
y: Optional[int] = 1,
width: Optional[int] = 200,
height: Optional[int] = 200,
radius: Optional[Union[Tuple[int, int, int, int], int]] = 10,
border_color: Optional[Union[Tuple[str, str, str, str], str]] = 'black',
border_width: Optional[Union[Tuple[int, int, int, int], int]] = 1):
def check_tuple(value):
if isinstance(value, tuple):
return value
else:
return value, value, value, value
_radius = check_tuple(radius)
_border_color = check_tuple(border_color)
_border_width = check_tuple(border_width)
# Top-Left Arc
canvas.create_arc(x, y, x + 2 * _radius[0], y + 2 * _radius[0],
start=90, extent=46, style="arc", outline=_border_color[0], width=_border_width[0])
# Top-Right Arc
canvas.create_arc(x + width - 2 * _radius[1], y, x + width, y + 2 * _radius[1],
start=45, extent=46, style="arc", outline=_border_color[0], width=_border_width[0])
# Right-Top Arc
canvas.create_arc(x + width - 2 * _radius[1], y, x + width, y + 2 * _radius[1],
start=0, extent=46, style="arc", outline=_border_color[1], width=_border_width[1])
# Right-Bottom Arc
canvas.create_arc(x + width - 2 * _radius[2], y + height - 2 * _radius[2], x + width, y + height,
start=315, extent=46, style="arc", outline=_border_color[1], width=_border_width[1])
# Bottom-Right Arc
canvas.create_arc(x + width - 2 * _radius[2], y + height - 2 * _radius[2], x + width, y + height,
start=270, extent=46, style="arc", outline=_border_color[2], width=_border_width[2])
# Bottom-Left Arc
canvas.create_arc(x, y + height - 2 * _radius[3], x + 2 * _radius[3], y + height,
start=225, extent=46, style="arc", outline=_border_color[2], width=_border_width[2])
# Left-Bottom Arc
canvas.create_arc(x, y + height - 2 * _radius[3], x + 2 * _radius[3], y + height,
start=180, extent=46, style="arc", outline=_border_color[3], width=_border_width[3])
# Left-Top Arc
canvas.create_arc(x, y, x + 2 * _radius[0], y + 2 * _radius[0],
start=135, extent=46, style="arc", outline=_border_color[3], width=_border_width[3])
# Top Line
canvas.create_line(x + _radius[0], y, x + width - _radius[1], y,
fill=_border_color[0], width=_border_width[0])
# Right Line
canvas.create_line(x + width, y + _radius[1], x + width, y + height - _radius[2],
fill=_border_color[1], width=_border_width[1])
# Bottom Line
canvas.create_line(x + _radius[3], y + height, x + width - _radius[2], y + height,
fill=_border_color[2], width=_border_width[2])
# Left Line
canvas.create_line(x, y + _radius[0], x, y + height - _radius[3],
fill=_border_color[3], width=_border_width[3])
如何用途:
root = tkinter.Tk()
root.geometry('500x500')
_canvas = tkinter.Canvas(root)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
_canvas.grid(sticky='nswe')
rounded_rect(_canvas, 5, 5, 300, 300, (0, 10, 20, 30), ('black', 'red', 'green', 'blue'))
root.mainloop()
它看起来如何:
它基本上与tobias_k回答相同的代码,我只添加了从颜色元组.
我不能使用创建多边形,因为它不给予我一个方法来改变每个边界的边界颜色,我试图填充多边形,但它不工作,圆角看起来不同。
1条答案
按热度按时间isr3a4wc1#
其中一种方法是将圆角矩形的内部区域划分为多个部分:4个角、4个侧条和内部矩形。然后你可以使用canvas函数填充这些部分:
结果: