我设法找到了一个在tkinter为圆形按钮制作课程的人,这个课程非常有效.然而,我的问题是,总是有一个白色的边界.

下面是代码的节选,我已经尽可能地将其格式化:

from tkinter import Tk, Canvas


class RoundedButton(Canvas):
    def __init__(self, master=None, text: str = "", radius=25, btnforeground="#007CEE", btnbackground="#ffffff",
                 clicked=None, font=("Righteous", 25), *args, **kwargs):
        super(RoundedButton, self).__init__(master, *args, **kwargs)
        self.config(bg=self.master["bg"])
        self.btnbackground = btnbackground
        self.btnforeground = btnforeground
        self.clicked = clicked

        self.radius = radius

        self.rect = self.round_rectangle(0, 0, 0, 0, tags="button", radius=radius, fill=btnbackground)
        self.text = self.create_text(0, 0, text=text, tags="button", fill=btnforeground, font=font,
                                     justify="center")

        self.tag_bind("button", "<ButtonPress>", self.border)
        self.tag_bind("button", "<ButtonRelease>", self.border)
        self.bind("<Configure>", self.resize)

        text_rect = self.bbox(self.text)
        if int(self["width"]) < text_rect[2] - text_rect[0]:
            self["width"] = (text_rect[2] - text_rect[0]) + 10

        if int(self["height"]) < text_rect[3] - text_rect[1]:
            self["height"] = (text_rect[3] - text_rect[1]) + 10

    def round_rectangle(self, x1, y1, x2, y2, radius=25, update=False,
                        **kwargs):  # if update is False a new rounded rectangle's id will be returned else updates existing rounded rect.
        # source: https://stackoverflow.com/a/44100075/15993687
        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]

        if not update:
            return self.create_polygon(points, **kwargs, smooth=True)

        else:
            self.coords(self.rect, points)

    def resize(self, event):
        text_bbox = self.bbox(self.text)

        if self.radius > event.width or self.radius > event.height:
            radius = min((event.width, event.height))

        else:
            radius = self.radius

        width, height = event.width, event.height

        if event.width < text_bbox[2] - text_bbox[0]:
            width = text_bbox[2] - text_bbox[0] + 30

        if event.height < text_bbox[3] - text_bbox[1]:
            height = text_bbox[3] - text_bbox[1] + 30

        self.round_rectangle(5, 5, width - 5, height - 5, radius, update=True)

        bbox = self.bbox(self.rect)

        x = ((bbox[2] - bbox[0]) / 2) - ((text_bbox[2] - text_bbox[0]) / 2)
        y = ((bbox[3] - bbox[1]) / 2) - ((text_bbox[3] - text_bbox[1]) / 2)

        self.moveto(self.text, x, y)

    def border(self, event):
        if event.type == "4":
            self.itemconfig(self.rect, fill="#DE8500")
            self.itemconfig(self.text, fill='#ffffff')
            if self.clicked is not None:
                self.clicked()

        else:
            self.itemconfig(self.rect, fill=self.btnbackground)
            self.itemconfig(self.text, fill=self.btnforeground)


window = Tk()
window.geometry("1440x872")
window.configure(bg="#007CEE")

download_button_1 = RoundedButton(
    text="Download",
    font=("Righteous", 30),
    borderwidth=0)
download_button_1.place(
    x=409,
    y=383,
    width=621.0,
    height=105.0
)

window.mainloop()

编辑:Furas的答案非常完美,对于任何有兴趣使用这个圆形按钮类的人,我建议将splinesteps更改为更高的self 价值.创建_多边形以帮助平滑

return self.create_polygon(points, **kwargs, smooth=True, splinesteps=10000)

推荐答案

Linux对我有用

highlightthickness=0

或者设置背景 colored颜色

highlightbackground="#007CEE"

最后

borderwidth=-1

Doc for Canvas(第effbot.org页,共archive.org页)显示了所有配置选项.


之前:

enter image description here

之后:

enter image description here

Python相关问答推荐

如何修复fpdf中的线路出血

取相框中一列的第二位数字

Django序列化器没有验证或保存数据

Polars -转换为PL后无法计算熵.列表

如何根据条件在多指标框架上进行groupby

Python多处理:当我在一个巨大的pandas数据框架上启动许多进程时,程序就会陷入困境

带条件计算最小值

对整个 pyramid 进行分组与对 pyramid 列子集进行分组

未删除映射表的行

如何列举Pandigital Prime Set

当递归函数的返回值未绑定到变量时,非局部变量不更新:

joblib:无法从父目录的另一个子文件夹加载转储模型

需要帮助重新调整python fill_between与数据点

使用Python查找、替换和调整PDF中的图像'

如何在达到end_time时自动将状态字段从1更改为0

为什么if2/if3会提供两种不同的输出?

寻找Regex模式返回与我当前函数类似的结果

跳过嵌套JSON中的级别并转换为Pandas Rame

为什么在FastAPI中创建与数据库的连接时需要使用生成器?

通过追加列表以极向聚合