Glade GTK TextBuffer change or set color

2k Views Asked by At

I have a processwindow which include a textview. The Textview has a textbuffer. With the following code i can print some text in this textbuffer-->textview--processwindow.

I want to change the color of the TextBuffer. How i can do this ?

import pygtk
import gtk
import gtk.glade


def __init__(self):

    self.gladefile                  = "XXXXX.glade"
    self.builder                    = gtk.Builder()
    self.builder.add_from_file(self.gladefile)
    self.builder.connect_signals(self)
    self.window                     = self.builder.get_object("window1")
    self.textbuffer                 = self.builder.get_object("textbuffer1")


def Print(self, text):

    self.textfeld = self.textfeld + '\n' + text 
    self.textbuffer.set_text(self.textfeld)
2

There are 2 best solutions below

0
user3231222 On BEST ANSWER

I found the solution myself. For my Solution you need "import Pango".

import pygtk
import gtk
import gtk.glade
import Pango


def __init__(self):
  self.gladefile                  = "XXXXX.glade"
  self.builder                    = gtk.Builder()
  self.builder.add_from_file(self.gladefile)
  self.builder.connect_signals(self)
  self.window                     = self.builder.get_object("window1")
  self.textbuffer                 = self.builder.get_object("textbuffer1")
  self.e_tag = self.textbuffer.create_tag("err", foreground="#FF0000")


def Print(self, text):

  self.textfeld = text + '\n'
  position = self.textbuffer.get_end_iter()
  self.textbuffer.insert_with_tags(position, self.textfeld, self.e_tag)

Note: .set_text() is not the same like .inser_with_tags, therefore i changed the self.textfeld command.

0
Jussi Kukkonen On

You may want to read the Text Widget Overview. The short answer from that page is this:

There are two ways to affect text attributes in GtkTextView. You can change the default attributes for a given GtkTextView, and you can apply tags that change the attributes for a region of text

So if you want to change the color of all the text, use Widget.override_color () (or for legacy GTK+ Widget.modify_text () and friends) on the TextView. If you only want to modify parts of the text, use TextBuffer tags. The Overview shows examples of both.