By using config() we can access the object's attributes after its initialisation.
Note: config() and configure() are same.
Tkinter configure method to manage the options of any object after its initialization with examples
Managing options of any object
We can manage any options of the Tkinter object by using config(). Here is the code to change the background colour of the button.
b1.configure(background='yellow')
Updating multiple options
Here below code both lines about button b1 give same result.
b1 = tk.Button(my_w) # Only object is created with default options
b1.configure(text='Hi Welcome',width=20,fg='red') #Options r changed.
b1.grid(row=2,column=2,padx=20,pady=30)
We will get one dictionary with options as keys with values. Here b1 is our button we declared in above code. We used type() to get the Data type of the object.
Any particular option's value we can display like this.
print(b1['width']) # value of the option = 20
Listing all values of the option
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
----
----
Checking availability of the attribute
if "bitmap" in l2.config():
#if "bitmap" in sb1.config():
print("Attribute is available")
else:
print("Attribute is Not available")
Example 1
Place four buttons in a window with different background colour. On Click of the button the window background colour should change to button background colour.
Create one button which will have same background colour of the window. If the colour of the window is changed, then the background colour of the button should also change.
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()