如何在 Tkinter 中更新 Button 小元件?


我們可以透過多種方式更新 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()

輸出

執行後,它首先會顯示以下視窗 −

當您單擊“增大按鈕大小”時,它將產生以下輸出 −

單擊“更改背景顏色”後,它將產生以下輸出 −

如果您單擊“移除邊框”,它將產生以下輸出 −

更新於: 2021 年 10 月 26 日

2 千 + 瀏覽量

開啟你的 職業生涯

完成課程以獲得認證

開始使用
廣告
© . All rights reserved.