Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am creating a program using Python and Gtk. I want to guide the user by pointing out mistakes if any. I have attached a sample glade interface below. Here the user is supposed to enter a valid station name in the GtkEntry field. I want to highlight this field with red color (for instance) if he presses the submit button.

enter image description here

How do I achieve this in GTK3 with Python?

share|improve this question
up vote 4 down vote accepted

First, you will need the Color class from Gdk and the Gtk class:

from gi.repository import Gtk
from gi.repository.Gdk import Color

Then, in your handler, change the foreground color of the text field:

COLOR_INVALID = Color(50000, 0, 0) // A dark red color
text_field.modify_fg(Gtk.StateFlags.NORMAL, COLOR_INVALID)

If you want to change the color back to the default when the text field is modified, put this in the change handler:

text_field.modify_fg(Gtk.StateFlags.NORMAL, None)

The RBG values for the Color class are in a range from 0-65535.

For more information, see the PyGTK documentation, specifically the Widget and Color classes.

share|improve this answer
Thank you very much. Can I ask if you know of a documentation source where I can find more such functions. Also this changes the color of the text. But is there a way to change the background as well? – nik90 Feb 6 '13 at 19:58
@Nik see my updated answer. I don't know how to change the background, you could try searching the documentation. – iBelieve Feb 7 '13 at 20:19
I can't try it myself since I just reinstalled Ubuntu and don't have the dev tools yet, but maybe you could try modify_bg instead of modify_fg. – iBelieve Feb 7 '13 at 20:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.