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

tkinterpythongui-programming

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

示例


# 导入所需的库
from tkinter import *
from tkinter import ttk

# 创建 tkinter 框架的实例
win = Tk()

# 定义窗口的几何形状
win.geometry("700x300")

# 增加按钮大小的函数
def Button_Size():
button1.configure(font=('Calibri 20 bold'))

# 更改背景颜色的函数
def Button_Color():
button2.configure(bg='green')

# 删除边框的函数
def Button_Border():
button3.configure(borderwidth=0)

# 第一个按钮
button1=Button(win, text="Increase the Button Size",
command=Button_Size)
button1.pack(pady=20)

# 第二个按钮
button2=Button(win, text="Change the Background Color",
command=Button_Color)
button2.pack(pady=20)

# 第三个按钮
button3 = Button(win, text="Remove the Border",
command=Button_Border)
button3.pack(pady=20)

win.mainloop()

输出

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

单击"增加按钮大小"时,将产生以下输出 −

单击"更改背景颜色"后,将产生以下输出 −

如果单击"删除边框",将产生以下输出 −


相关文章