如何設定某些 Tkinter 控制元件的邊框顏色?


我們假設想要更改 Tkinter 控制元件的邊框顏色。我們可以透過傳遞控制元件的 highlightcolor, highlightbackground 屬性來配置控制元件。

示例

在此示例中,我們建立了一個 Entry 控制元件和一個按鈕,可以觸發以更改 Entry 控制元件的邊框顏色。

#Import the required libraries
from tkinter import *

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

#Set the geometry of frame
win.geometry("600x250")

#Define a function to change the color of entry widget
def change_color():
   text.config(highlightthickness=2, highlightbackground="red")

#Create a Entry widget for which we want to change the border color
text= Entry(win, width= 50)
text.pack()

#Create a Button Widget
button= Button(win, text= "Change", command=change_color)
button.pack(pady=20)

win.mainloop()

輸出

執行以上程式碼將顯示一個視窗,其中包含一個按鈕,可用於更改輸入控制元件的邊框顏色。

現在單擊“更改”按鈕來更改控制元件的邊框顏色。

更新日期:2021 年 3 月 26 日

3K+ 次瀏覽

開啟你的 事業

完成課程獲得認證

開始吧
廣告