如何制作带圆角的 tkinter 画布矩形? [英] How to make a tkinter canvas rectangle with rounded corners?

查看:70
本文介绍了如何制作带圆角的 tkinter 画布矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to create a rectangle with rounded corners. I'm using canvas from tkinter.

推荐答案

为 tobias 的方法提供另一种方法是确实用一个多边形来做.

Offering an alternate approach to tobias's method would be to indeed do it with one polygon.

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

This would have the advantage of being one canvas object if you are worried about optimization, or not having to worry about a tag system for referring to a single object.

代码有点长,但非常基本,因为它只是利用了这样一个想法,即在平滑多边形时,您可以给相同的坐标两次以阻止"平滑的发生.

The code is a bit longer, but very basic, as it is just utilizing the idea that when smoothing a polygon, you can give the same coordinate twice to 'stop' the smooth from occuring.

这是一个可以做什么的例子:

This is an example of what can be done:

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_ 一样> 方法.

Using this function, you can just provide the normal coordinates that you would to a rectangle, and then specify the 'radius' which is rounded in the corners. The use of **kwargs denotes that you can pass keyword arguments such as fill="blue", just as you usually could with a create_ method.

虽然坐标看起来很复杂,但它只是有条不紊地绕过矩形"中的每个点,给每个非角点两次.

Although the coords look complex, it is just going around methodically to each point in the 'rectangle', giving each non-corner point twice.

如果你不介意一行很长的代码,你可以把所有的坐标放在一行,让函数只有两行(!).这看起来像:

If you didn't mind a rather long line of code, you could put all the coordinates on one line, making the function just 2 lines(!). This looks like:

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)

这会产生以下结果(请注意,这是一个画布对象):

This produces the following (Note in mind this is ONE canvas object):

如果你想在矩形创建后更新它的位置,你可以使用这样的函数(如果与原始canvas对象在同一范围内):

If you want to update the position of the rectangle after it has been created, you could use a function like this (if in the same scope as the original canvas object):

def update_rectangle_coords(round_rect, x1, y1, x2, y2, r=25):
    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)
    canvas.coords(round_rect, *points)

因此,要更新 my_rectangle 的位置(来自第一个代码示例),我们可以说:

So, to update my_rectangle's position (from the first code example), we could say:

update_rectangle_coords(my_rectangle, 20, 20, 100, 100)

这篇关于如何制作带圆角的 tkinter 画布矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆