如何重置 Python Tkinter 按鈕的背景色?


Tkinter 按鈕對於處理應用程式中的事件非常有用。我們可以使用預定義的屬性來配置按鈕屬性,例如文字樣式、字體系列、背景色、文字顏色和文字大小。

我們可以透過定義回撥函式來重置背景色和其他屬性。

示例

#Import the tkinter library
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame
win= Tk()
#Define the geometry of the function
win.geometry("750x250")
#Define a function to change the properties of button
def change_color():
   btn.configure(bg="OrangeRed3", fg= "white")
#Create a Label
Label(win, text= "Click the Button to reset the Color of the Button", font= ('Georgia 16 italic')).pack(pady=30)
#Create a button to close the window
btn = Button(win, text ="RESET", command=change_color, font=('Georgia 11'))
btn.pack(side=TOP, ipady=5, ipadx=20)
win.mainloop()

輸出

執行以上程式碼會顯示一個包含按鈕和文字的視窗。

現在,點選“重置”按鈕來更改按鈕的背景色以及前景顏色。

更新於: 2021 年 4 月 22 日

2K+ 瀏覽

開啟你的 事業

完成課程並獲得認證

開始
廣告