Change Tkinter widget options after creation using config() or configure(). Both method names work the same way. Use cget() or widget['option'] to read the current value.
For example, b1.config(text='Updated', bg='yellow') changes a button's label and background without creating a new button.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x150")
b1 = tk.Button(my_w, text="Before", width=15)
b1.pack(pady=25)
b1.configure(text="After", bg="yellow", fg="black")
my_w.mainloop()
config() and configure() are aliases. Calling either with keyword arguments changes options; calling either with no arguments returns a dictionary describing supported options.After creating a Button named b1, change its background without rebuilding it:
b1.configure(background='yellow')
The shorter b1.config(bg='yellow') has the same effect. Commonly used options include text, width, font, fg (foreground/text colour), bg (background) and state. Supported options differ by widget type; see Tkinter widget attributes.
You can supply several keyword arguments in the same call. This example creates a button with default settings and then changes its text, width and foreground colour:
b1 = tk.Button(my_w) # Create with default options
b1.configure(text='Hi Welcome', width=20, fg='red')
b1.grid(row=2, column=2, padx=20, pady=30)
The same initial appearance can be set directly when the widget is created:
b1 = tk.Button(my_w, text='Hi Welcome', width=20, fg='red')
b1.grid(row=2, column=2, padx=20, pady=30)
Use configure() when the appearance or behaviour must change later—for example, after a user clicks a button or enters data. Both snippets assume tk has been imported and my_w is an existing window.
Calling config() without arguments returns a dictionary of option details. Use type() to see its data type:
print(type(b1.config())) # <class 'dict'>
The keys() method returns the names of the options; convert them to a list if you need a Python list:
print(b1.config().keys())
option_names = list(b1.config().keys())
print("Number of supported options:", len(option_names))
Sample output (shortened): the exact set varies by operating system, Tk version and widget type.
dict_keys(['activebackground', 'activeforeground', 'anchor', 'background', 'bd', 'bg', 'command', 'font', 'text', 'width', ...])
To list each option on its own line:
for option in b1.config():
print(option)
Use either widget['option'] or widget.cget('option') to read a widget's current setting. If b1 was created with width=20, both calls below return that configured width:
print(b1['width'])
print(b1.cget('width'))
print(b1.cget('text'))
For detailed configuration metadata (including the default and current values), pass the option name to config(). Tkinter returns a tuple for a standard option:
print(b1.config('width'))
The tuple may contain the option name, database names, default value and current value. For a single current value, cget() is simpler.
Loop through the available option names and retrieve their values. This is useful when inspecting a widget during development:
for option in b1.config():
print(f"{option}: {b1.cget(option)}")
Illustrative output: colours such as SystemButtonFace and default settings depend on the platform.
activebackground: SystemButtonFace
anchor: center
background: green
bd: 2
bg: green
...
Not every Tkinter widget supports every option. Check the widget's configuration dictionary before trying to set an option:
if 'bitmap' in b1.config():
print('Option is available')
else:
print('Option is not available')
This example uses b1, which was defined earlier. A widget such as a Label, Text or Entry may expose a different set of options. Test the specific widget you plan to configure instead of assuming an option works everywhere.
Store the settings in a dictionary and unpack them using **. This is helpful when applying a reusable collection of formatting options:
settings = {
'text': 'Updated Label',
'bg': 'lightgreen',
'fg': 'black',
'width': 20
}
b1.configure(**settings)
You can also update the enabled state of a widget. For example, disable a button temporarily and enable it again after 1.5 seconds:
b1.config(state='disabled')
my_w.after(1500, lambda: b1.config(state='normal'))
Each button uses a different background colour. When clicked, its command calls my_upd(colour), which updates the parent window using config(background=colour).

import tkinter as tk
my_w = tk.Tk()
my_w.geometry('410x200')
font1 = ('times', 14, 'bold')
def my_upd(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()
How it works: command=lambda: my_upd(...) passes a colour string when the corresponding button is pressed. my_w.config(background=colour) changes the window immediately. This is the same interaction demonstrated in the screenshot and video.
Read the window's current background with my_w['background'] and pass it into the button's bg option. Both objects start with the same colour:

import tkinter as tk
my_w = tk.Tk()
my_w.geometry('400x300')
font1 = ('times', 16, 'bold')
my_w.config(background='#FFCC00')
b1 = tk.Button(my_w, text='Hi Welcome', font=font1,
width=20, bg=my_w['background'])
b1.grid(row=2, column=2, padx=20, pady=30)
my_w.mainloop()
bg=my_w['background'] copies the colour at the time the button is created. It does not create an automatic binding. If the window changes colour later, update the button separately.To keep their backgrounds matched after a later change, configure both widgets inside the same callback. The following independent program alternates between two colours:
import tkinter as tk
my_w = tk.Tk()
my_w.geometry('350x180')
my_w.config(bg='#FFCC00')
b1 = tk.Button(my_w, text='Change colour', width=20,
bg=my_w.cget('bg'))
b1.pack(pady=35)
def change_colour():
colour = '#99DDFF' if my_w.cget('bg') == '#FFCC00' else '#FFCC00'
my_w.config(bg=colour)
b1.config(bg=colour)
b1.config(command=change_colour)
my_w.mainloop()
| Problem | Likely cause and solution |
|---|---|
_tkinter.TclError: unknown option | The widget does not support that option. Check widget.config().keys() and use the correct option name. |
NameError: name 'b1' is not defined | The code uses b1 before the widget is created. Run the complete example or create the widget first. |
Changing bg seems ineffective on ttk widgets | Many themed ttk widgets use ttk.Style rather than ordinary widget colour options. Use the style system for themed widget appearance. |
| The button does not change when the window colour changes | The colour was copied only once. Update both widgets in the same function, as in the dynamic example above. |
| Unexpected option values on Windows, Linux or macOS | System colours and defaults depend on the Tk theme and operating system. Inspect the running widget with cget(). |
No. They refer to the same method. Both can update widget options, return option details or list supported options.
Use widget.cget('bg'), widget.cget('text'), or dictionary-style access such as widget['text'].
Yes. For example, b1.configure(text='Next', bg='yellow', fg='black'). You can also pass a dictionary with **settings.
Standard Tkinter widgets support config(), but the set of available options varies. Themed ttk widgets often need ttk.Style for colours and other visual styling.
Read and save the previous value before changing it. You can restore that saved value later:
previous_bg = b1.cget('bg')
b1.config(bg='yellow')
# Restore the original background when required:
b1.config(bg=previous_bg)
Continue with widget attributes and options, Button widgets, changing Entry backgrounds, and the Tkinter project examples.
Text Button Entry How config() is used to manage background colourAuthor & 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.