python - tkinter 圆形按钮有白色边框

标签 python user-interface tkinter geometry

我设法找到了一个在 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.create_polygon 的更高值以帮助平滑

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

最佳答案

在 Linux 上对我有用

highlightthickness=0

或设置背景颜色

highlightbackground="#007CEE"

最终

borderwidth=-1

Canvas 的文档(archive.org 上的 effbot.org 页面)显示所有配置选项。


之前:

enter image description here

之后:

enter image description here

关于python - tkinter 圆形按钮有白色边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71430948/

相关文章:

python - 在 Python 中年份超出范围?

python - 如何对DataFrame的列进行分类和计数?

python - 尽管未收集,tkinter 图像仍不显示

python - 按钮的 Tkinter 命令不起作用

python - Python Tkinter 中.xxxxxxx 的含义是什么

python - 如何使用 matplotlib 的动画编写器加速 MP4 的生成?

python - 如何获取所有文档类型的列表

user-interface - 在没有窗口/GUI 的情况下运行 Pygame

c - 如何在 GUI 窗口中创建 C 程序?

c - 用C语言编写的Windows