Python tkinter config()

By using config() we can access the object's attributes after its initialisation.

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.

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 = tk.Button(my_w,text='Hi  Welcome',width=20,fg='red')

Listing all options of the object

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.
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 the object.
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'])

Value of the option

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")
    print("Attribute is Not available")

Example 1

Buttons to update background colour of window
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.
import tkinter as tk
my_w = tk.Tk()
font1=('times', 14, 'bold')
def my_upd(colour): # to update the window background colour. 
b1 = tk.Button(my_w,text='#FFFF00',font=font1,

b2 = tk.Button(my_w,text='#00FF00',font=font1,

b3 = tk.Button(my_w,text='#FF0000',font=font1,

b4 = tk.Button(my_w,text='#0000FF',font=font1,


Example 2

Buttons bg value same as windows 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()
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

Text Button Entry How config() is used to manage background colour

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer