python-3.x Tkinter中的圆角矩形,我可以填充内部,并改变颜色,从直到边界

5fjcxozz  于 2023-05-30  发布在  Python
关注(0)|答案(1)|浏览(235)

好吧,我会努力解释得更好。我需要使用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回答相同的代码,我只添加了从颜色元组.
我不能使用创建多边形,因为它不给予我一个方法来改变每个边界的边界颜色,我试图填充多边形,但它不工作,圆角看起来不同。

isr3a4wc

isr3a4wc1#

其中一种方法是将圆角矩形的内部区域划分为多个部分:4个角、4个侧条和内部矩形。然后你可以使用canvas函数填充这些部分:

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,
                 fill_color: Optional[str] = 'yellow'):  # added fill_color
     ...
    # fill top-left corner
    canvas.create_arc(x, y, x + 2 * _radius[0], y + 2 * _radius[0],
                      start=90, extent=46, fill=fill_color, outline='')
    # fill top-right corner
    canvas.create_arc(x + width - 2 * _radius[1], y, x + width, y + 2 * _radius[1],
                      start=0, extent=90, fill=fill_color, outline='')
    # fill bottom-right corner
    canvas.create_arc(x + width - 2 * _radius[2], y + height - 2 * _radius[2], x + width, y + height,
                      start=270, extent=90, fill=fill_color, outline='')
    # fill bottom-left corner
    canvas.create_arc(x, y + height - 2 * _radius[3], x + 2 * _radius[3], y + height,
                      start=180, extent=90, fill=fill_color, outline='')
    # fill top bar
    h1 = max(_radius[0], _radius[1])
    canvas.create_rectangle(x+_radius[0], y, x+width-_radius[1], y+h1, fill=fill_color, outline='')
    # fill bottom bar
    h2 = max(_radius[2], _radius[3])
    canvas.create_rectangle(x+_radius[3], y+height-h2, x+width-_radius[2], y+height, fill=fill_color, outline='')
    # fill left bar
    w1 = max(_radius[0], _radius[3])
    canvas.create_rectangle(x, y+_radius[0], x+w1, y+height-_radius[3], fill=fill_color, outline='')
    # fill right bar
    w2 = max(_radius[1], _radius[2])
    canvas.create_rectangle(x+width-w2, y+_radius[1], x+width, y+height-_radius[2], fill=fill_color, outline='')
    # fill internal rectangle
    canvas.create_rectangle(x+w1, y+h1, x+width-w2, y+height-h2, fill=fill_color, outline='')

    # then draw the outline
    ...

结果:

相关问题