Python tkinter config()

Basics of Python Tkinter
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.
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)
b1 = tk.Button(my_w,text='Hi  Welcome',width=20,fg='red')
b1.grid(row=2,column=2,padx=20,pady=30)

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

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
----
----

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()
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()

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()
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()
Text Button Entry How config() is used to manage background colour
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    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-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer