b1.configure(background='yellow')
b1 = tk.Button(my_w) # Only object is created with default options
b1.configure(text='Hi Welcome',width=20,fg='red') #Options are changed.
b1.grid(row=2,column=2,padx=20,pady=30)
b1 = tk.Button(my_w,text='Hi Welcome',width=20,fg='red')
b1.grid(row=2,column=2,padx=20,pady=30)
print(type(b1.config())) # dictionary <class 'dict'>
As we are getting one dictionary, so we can use the keys() method to get all the keys or options as a list of all widget attributes.
print(b1.config().keys())
The output is here .
dict_keys(['activebackground', 'activeforeground', 'anchor',
'background', 'bd', 'bg', 'bitmap', 'borderwidth', 'command', 'compound',
'cursor', 'default', 'disabledforeground', 'fg', 'font', 'foreground',
'height', 'highlightbackground', 'highlightcolor', 'highlightthickness',
'image', 'justify', 'overrelief', 'padx', 'pady', 'relief', 'repeatdelay',
'repeatinterval', 'state', 'takefocus', 'text', 'textvariable', 'underline',
'width', 'wraplength'])
print(b1['width']) # value of the option = 20
for options in b1.config():
print(options + ": " + str(b1[options]))
Output ( more are there ... )
activebackground: SystemButtonFace
activeforeground: SystemButtonText
anchor: center
background: green
bd: 2
bg: green
----
----
if "bitmap" in l2.config():
#if "bitmap" in sb1.config():
print("Option is available")
else:
print("Option is Not available")

import tkinter as tk
my_w = tk.Tk()
my_w.geometry("410x200")
font1=('times', 14, 'bold')
def my_upd(colour): # to update the window background colour.
my_w.config(background=colour)
b1 = tk.Button(my_w,text='#FFFF00',font=font1,
bg='#FFFF00',command=lambda:my_upd('#FFFF00'))
b1.grid(row=0,column=0,padx=3,pady=30)
b2 = tk.Button(my_w,text='#00FF00',font=font1,
bg='#00FF00',command=lambda:my_upd('#00FF00'))
b2.grid(row=0,column=1,padx=3,pady=30)
b3 = tk.Button(my_w,text='#FF0000',font=font1,
bg='#FF0000',command=lambda:my_upd('#FF0000'))
b3.grid(row=0,column=2,padx=3,pady=30)
b4 = tk.Button(my_w,text='#0000FF',font=font1,
bg='#0000FF',command=lambda:my_upd('#0000FF'))
b4.grid(row=0,column=3,padx=3,pady=30)
my_w.mainloop()

import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x300")
font1=('times', 16, 'bold')
my_w.config(background='#FFCC00') # background colour of window
b1 = tk.Button(my_w,text='Hi Welcome',font=font1,
width=20,bg=my_w['background']) # bg set to value of window
b1.grid(row=2,column=2,padx=20,pady=30)
my_w.mainloop()
We can read any option value of a widget directly using
widget['option_name'] syntax — same as reading a value from a
dictionary. So my_w['background']
reads the current background colour of the window and assigns it to the button.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.