Python Tkinter configure() — Manage Widget Options After Initialisation

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()
Key point: config() and configure() are aliases. Calling either with keyword arguments changes options; calling either with no arguments returns a dictionary describing supported options.

Jump to tutorial contents

Video: Tkinter config() and configure() examples

Tkinter configure method to manage the options of any object after its initialization with examples

Managing one widget option ↑

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.

Updating multiple options ↑

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.

Listing all options of a widget ↑

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)

Reading the value of an 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.

Listing all current option values ↑

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

Checking whether an option is available ↑

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.

Updating options using a dictionary ↑

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

Example 1: Four buttons change the window background ↑

Each button uses a different background colour. When clicked, its command calls my_upd(colour), which updates the parent window using config(background=colour).

Four Tkinter colour buttons that change the parent window background when clicked
Click a colour button to update the window background.

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.

Example 2: Match a button's background to its window ↑

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:

Tkinter button using the same background colour as its parent window
The button takes its initial colour from the window configuration.

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()
Important distinction: 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.

Keeping a button and window colour in sync ↑

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

Common errors and troubleshooting ↑

ProblemLikely cause and solution
_tkinter.TclError: unknown optionThe widget does not support that option. Check widget.config().keys() and use the correct option name.
NameError: name 'b1' is not definedThe code uses b1 before the widget is created. Run the complete example or create the widget first.
Changing bg seems ineffective on ttk widgetsMany 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 changesThe 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 macOSSystem colours and defaults depend on the Tk theme and operating system. Inspect the running widget with cget().

Frequently asked questions ↑

Is there any difference between config() and configure() in Tkinter?

No. They refer to the same method. Both can update widget options, return option details or list supported options.

How can I read a widget's present colour or text?

Use widget.cget('bg'), widget.cget('text'), or dictionary-style access such as widget['text'].

Can I change multiple widget options with one command?

Yes. For example, b1.configure(text='Next', bg='yellow', fg='black'). You can also pass a dictionary with **settings.

Does config() work on all Tkinter widgets?

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.

Can I restore a previous widget option?

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 colour

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com







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 Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer