Tkinter Text Widget Background Color

Peter Otten __peter__ at
Tue Feb 22 06:50:06 EST 2011

Sathish S wrote:

> Hi Ppl,
> I'm using the Tkinter Text Widget in my user interface. I'm trying to
> create a blinking effect for this Text Widget. I saw from the
> documentation I can set the color if the widget when I create it.
> x=Text(root,bg='#CFF')
> However, I couldn't find any property or function that set's the
> background color. Is there any way to set the back ground color after the
> object is created.
> Thanks,
> Sathish

import Tkinter as tk
from itertools import cycle

root = tk.Tk()
text = tk.Text(root, font=("Helvetica", 70))

text.insert(tk.END, "Hello, geocities")
text.tag_add("initial", "1.0", "1.1")
text.tag_add("initial", "1.7", "1.8")

colors = cycle("red yellow blue".split())
initial_colors = cycle("#8f8 #f08".split())

def switch_color():
    # change the complete widget's background color
    text["bg"] = next(colors)

    # change the background color of tagged portions
    # of the widget's conten
    text.tag_config("initial", background=next(initial_colors))

    # do it again after 300 milliseconds
    root.after(300, switch_color)

# call the color-setting function manually the first time

Does this help?

More information about the Python-list mailing list