如何制作带圆角的tkinter帆布矩形?

时间:2017-05-21 17:08:21

标签: python python-3.x canvas tkinter tkinter-canvas

我想创建一个带圆角的矩形。我正在使用tkinter的画布。

3 个答案:

答案 0 :(得分:13)

为tobias的方法提供另一种方法是使用一个多边形来实现它。

如果您担心优化,或者不必担心引用单个对象的标记系统,这将具有成为一个canvas对象的优势。

代码有点长,但非常基本,因为它只是利用了这样的想法,即在平滑多边形时,你可以给出相同的坐标两次,以“停止”平滑发生。

这是可以做的事情的一个例子:

from tkinter import *
root = Tk()
canvas = Canvas(root)
canvas.pack()

def round_rectangle(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)

my_rectangle = round_rectangle(50, 50, 150, 100, radius=20, fill="blue")

root.mainloop()

使用此功能,您可以只提供矩形的法线坐标,然后指定角落圆角的“半径”。使用**kwargs表示您可以传递fill="blue"等关键字参数,就像通常使用create_方法一样。

尽管coords看起来很复杂,但它只是有条不紊地绕过'矩形'中的每个点,给每个非角点两次。

如果您不介意相当长的代码行,可以将所有坐标放在一行上,使函数只有2行(!)。这看起来像:

def round_rectangle(x1, y1, x2, y2, r=25, **kwargs):    
    points = (x1+r, y1, x1+r, y1, x2-r, y1, x2-r, y1, x2, y1, x2, y1+r, x2, y1+r, x2, y2-r, x2, y2-r, x2, y2, x2-r, y2, x2-r, y2, x1+r, y2, x1+r, y2, x1, y2, x1, y2-r, x1, y2-r, x1, y1+r, x1, y1+r, x1, y1)
    return canvas.create_polygon(points, **kwargs, smooth=True)

这会产生以下内容(注意这是一个canvas对象):

Rounded rectangle produced by function

答案 1 :(得分:5)

似乎没有内置的方法。最接近的是具有smooth=1的折线,但它仍然看起来更像旧的电视屏幕,两侧也略微弯曲。

相反,你可以定义一个辅助函数,结合直线和圆弧的圆角矩形:

def rounded_rect(canvas, x, y, w, h, c):
    canvas.create_arc(x,   y,   x+2*c,   y+2*c,   start= 90, extent=90, style="arc")
    canvas.create_arc(x+w-2*c, y+h-2*c, x+w, y+h, start=270, extent=90, style="arc")
    canvas.create_arc(x+w-2*c, y,   x+w, y+2*c,   start=  0, extent=90, style="arc")
    canvas.create_arc(x,   y+h-2*c, x+2*c,   y+h, start=180, extent=90, style="arc")
    canvas.create_line(x+c, y,   x+w-c, y    )
    canvas.create_line(x+c, y+h, x+w-c, y+h  )
    canvas.create_line(x,   y+c, x,     y+h-c)
    canvas.create_line(x+w, y+c, x+w,   y+h-c)

示例:

import tkinter
root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.pack()
rounded_rect(canvas, 20, 20, 60, 40, 10)
root.mainloop()

enter image description here

您还可以提供另一个**options参数来设置各个部分的线宽,颜色等,但问题是,例如直线和圆弧分别使用不同的线颜色参数(filloutline)。此外,如果您想要一个填充的圆角矩形,则必须使用多个矩形指定第二个方法。

答案 2 :(得分:3)

我知道此帖子已经有一个矩形的可接受答案。但是对于那些正在寻找具有圆角的多边形(显然包括矩形)的人,我根据@SneakyTutle的答案编写了此代码。

roundPolygon(x_array, y_array, sharpness, **kwargs)

结果

Image

其背后的逻辑是启用平滑点并将子点放置在顶点旁边。这样,只有拐角会变圆,多边形的其余部分保持平坦。

from tkinter import *
root = Tk()
canvas = Canvas(root, width = 1000, height = 1000)
canvas.pack()

def roundPolygon(x, y, sharpness, **kwargs):

    # The sharpness here is just how close the sub-points
    # are going to be to the vertex. The more the sharpness,
    # the more the sub-points will be closer to the vertex.
    # (This is not normalized)
    if sharpness < 2:
        sharpness = 2

    ratioMultiplier = sharpness - 1
    ratioDividend = sharpness

    # Array to store the points
    points = []

    # Iterate over the x points
    for i in range(len(x)):
        # Set vertex
        points.append(x[i])
        points.append(y[i])

        # If it's not the last point
        if i != (len(x) - 1):
            # Insert submultiples points. The more the sharpness, the more these points will be
            # closer to the vertex. 
            points.append((ratioMultiplier*x[i] + x[i + 1])/ratioDividend)
            points.append((ratioMultiplier*y[i] + y[i + 1])/ratioDividend)
            points.append((ratioMultiplier*x[i + 1] + x[i])/ratioDividend)
            points.append((ratioMultiplier*y[i + 1] + y[i])/ratioDividend)
        else:
            # Insert submultiples points.
            points.append((ratioMultiplier*x[i] + x[0])/ratioDividend)
            points.append((ratioMultiplier*y[i] + y[0])/ratioDividend)
            points.append((ratioMultiplier*x[0] + x[i])/ratioDividend)
            points.append((ratioMultiplier*y[0] + y[i])/ratioDividend)
            # Close the polygon
            points.append(x[0])
            points.append(y[0])

    return canvas.create_polygon(points, **kwargs, smooth=TRUE)

my_rectangle = roundPolygon([50, 350, 350, 50], [50, 50, 350, 350], 10 , width=5, outline="#82B366", fill="#D5E8D4")
my_triangle = roundPolygon([50, 650, 50], [400, 700, 1000], 8 , width=5, outline="#82B366", fill="#D5E8D4")

root.mainloop()

我想不出一种标准化锐度的好方法。无论如何,介于2到10之间的值对于任何情况都将是好的。随意更改代码。

仅出于可视化目的,对于具有 sharpness = 8 的三角形, for 循环的结果代码如下。您可能会注意到,如果清晰度为2,则子点将放置在顶点的中间。

points = [
      # Begin vertex
      x[0], y[0],
      # Between vertices
      (7*x[0] + x[1])/8, (7*y[0] + y[1])/8,
      (7*x[1] + x[0])/8, (7*y[1] + y[0])/8,
      # Vertex
      x[1], y[1],
      # Between vertices
      (7*x[1] + x[2])/8, (7*y[1] + y[2])/8,
      (7*x[2] + x[1])/8, (7*y[2] + y[1])/8,
      # Vertex
      x[2], y[2],
      # Between vertices
      (7*x[2] + x[0])/8, (7*y[2] + y[0])/8,
      (7*x[0] + x[2])/8, (7*y[0] + y[2])/8,
      # End/Begin vertex
      x[0], y[0]
    ]