Cth*_*lhu 4 python canvas tkinter python-3.x tkinter-canvas
我想创建一个圆角矩形.我正在使用tkinter的画布.
Sne*_*tle 14
为tobias的方法提供另一种方法是使用一个多边形来实现它.
如果您担心优化,或者不必担心引用单个对象的标记系统,这将具有作为一个画布对象的优点.
代码有点长,但非常基本,因为它只是利用了这样的想法:在平滑多边形时,你可以给出相同的坐标两次来"停止"平滑发生.
这是可以做的事情的一个例子:
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()
Run Code Online (Sandbox Code Playgroud)
使用此功能,您可以只提供矩形的法线坐标,然后指定角落中圆角的"半径".使用**kwargs表示您可以传递关键字参数fill="blue",就像通常使用create_方法一样.
虽然坐标看起来很复杂,但它只是有条不紊地绕过"矩形"中的每个点,给每个非角点两次.
如果您不介意相当长的代码行,可以将所有坐标放在一行上,使函数只有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)
Run Code Online (Sandbox Code Playgroud)
这会产生以下结果(注意这是一个canvas对象):
Fra*_*mes 14
我知道这篇文章已经有一个矩形的可接受答案。但对于那些寻找任何带有圆角的多边形(显然包括矩形)的人来说,我根据@SneakyTutle的答案编写了这段代码。
roundPolygon(x_array, y_array, sharpness, **kwargs)
Run Code Online (Sandbox Code Playgroud)
结果

这背后的逻辑是启用平滑并将子点放置在顶点旁边。这样,只有角才会被圆化,多边形的其余部分保持平坦。
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()
Run Code Online (Sandbox Code Playgroud)
我想不出一个好的方法来标准化清晰度。无论如何,2 到 10 之间的值对于任何情况都会有好处。请随意更改代码。
仅出于可视化目的,对于锐度=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]
]
Run Code Online (Sandbox Code Playgroud)
似乎没有内置的方法.最接近的是折线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)
Run Code Online (Sandbox Code Playgroud)
例:
import tkinter
root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.pack()
rounded_rect(canvas, 20, 20, 60, 40, 10)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
您还可以提供另一个**options参数来设置各个零件的线宽,颜色等,但问题是例如线和弧对线颜色(fill和outline分别)使用不同的参数.此外,如果要使用填充的圆角矩形,则必须使用多个矩形将其指定为第二种方法.