且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

圆角按钮 tkinter python

更新时间:2023-09-29 17:33:22

在 tkinter 中制作圆形按钮的一个非常简单的方法是使用图像.

A very easy way to make a rounded button in tkinter is to use an image.

首先创建您希望按钮看起来像的图像,将其另存为 .png 并删除外部背景,使其四舍五入,如下所示:

First create an image of what you want you button to look like save it as a .png and remove the outside background so it is rounded like the one below:

接下来将图像插入一个带有 PhotoImage 的按钮,如下所示:

Next insert the image in a button with PhotoImage like this:

self.loadimage = tk.PhotoImage(file="rounded_button.png")
self.roundedbutton = tk.Button(self, image=self.loadimage)
self.roundedbutton["bg"] = "white"
self.roundedbutton["border"] = "0"
self.roundedbutton.pack(side="top")

确保使用border="0",按钮边框将被移除.

Ensure to use border="0" and the button border will be removed.

我添加了 self.roundedborder["bg"] = "white" 以便按钮的背景与 Tkinter 窗口的背景相同.

I added the self.roundedborder["bg"] = "white" so that the the background the background of the button is the same as the Tkinter window.

最重要的是,您可以使用任何您喜欢的形状,而不仅仅是普通的按钮形状.

The great part is that you can use any shape you like not just the normal button shapes.