如何在 Tkinter 中更新按钮小部件?

我们可以通过多种方式更新 Tkinter 中的 Button 小部件,例如,我们可以更改其大小、更改其背景颜色或移除其边框等。在下面的示例中,我们将创建三个 Button 小部件和每个按钮,单击后,将调用不同的函数来更新其功能。

示例

# Import the required library
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame
win = Tk()

# Define geometry of the window
win.geometry("700x300")

# Function to Increase the Size of the Button
def Button_Size():
   button1.configure(font=('Calibri 20 bold'))

# Function to change the background color
def Button_Color():
   button2.configure(bg='green')

# Function to Remove Border
def Button_Border():
   button3.configure(borderwidth=0)

# First Button
button1=Button(win, text="Increase the Button Size",
command=Button_Size)
button1.pack(pady=20)

# Second Button
button2=Button(win, text="Change the Background Color",
command=Button_Color)
button2.pack(pady=20)

# Third Button
button3 = Button(win, text="Remove the Border",
command=Button_Border)
button3.pack(pady=20)

win.mainloop()
输出结果

执行后,它将首先显示以下窗口 -

当您单击“增加按钮大小”时,它将产生以下输出 -

单击“更改背景颜色”后,它将产生以下输出 -

如果单击“删除边框”,它将产生以下输出 -