ในการกำหนดค่าคุณสมบัติของวิดเจ็ตในแอปพลิเคชัน Tkinter โดยทั่วไปเราใช้ 'configure(**options) ' กระบวนการ. เราสามารถปรับแต่งสีพื้นหลัง คุณสมบัติแบบอักษร และคุณสมบัติเฉพาะอื่นๆ ของวิดเจ็ตในแอปพลิเคชันได้
อาจมีกรณีที่เราต้องการเปลี่ยนสีพื้นหลังของวิดเจ็ตแบบไดนามิก แต่เรายังสามารถกำหนดรายการสีและเปลี่ยนสีในขณะที่วนซ้ำในรายการได้
ตัวอย่าง
#Import the required libraries
from tkinter import *
from random import shuffle
import time
#Create an instance of Tkinter frame
win = Tk()
win.geometry("700x250")
#Add fonts for all the widgets
win.option_add("*Font", "aerial")
# Define the backround color for all the widgets
def change_color():
colors= ['#e9c46a','#e76f51','#264653','#2a9d8f','#e85d04','#a2d2ff','#06d6a0','#4d908e']
while True:
shuffle(colors)
for i in range(0,len(colors)):
win.config(background=colors[i])
win.update()
time.sleep(1)
#Display bunch of widgets
label=Label(win, text="Hello World", bg= 'white')
label.pack(pady= 40, padx= 30)
#Create a Button to change the background color of the widgets
btn=Button(win, text="Button", command= change_color)
btn.pack(pady= 10)
win.mainloop() ผลลัพธ์
เมื่อเราคอมไพล์โค้ดข้างต้น จะแสดงหน้าต่างที่มีวิดเจ็ตป้ายกำกับและปุ่ม

เมื่อเรากดปุ่ม มันจะเรียก change_color() ฟังก์ชันที่เปลี่ยนสีพื้นหลังของหน้าต่างแบบไดนามิก
