Reputation: 175
I am using a Gtk.TextBuffer()
inside a Gtk.TextView()
to write some text on the screen. I wish to change colors of the text while writing often. eg.
In Green -- Printing Green color
In Red -- Printing Red color
In Green -- Printing Green color
In Red -- Printing Red color
Can you please suggest some function to do this.
Upvotes: 0
Views: 3036
Reputation: 3538
Since GTK3.16 you can use pango markup.
self.textbuffer.insert_markup(iter, markup);
.
self.textbuffer.insert_markup(self.textbuffer.get_end_iter(), "<b>and some bold text</b>", -1)
StackOverflow answer with example
GTK3+ doc: https://developer.gnome.org/gtk3/stable/GtkTextBuffer.html#gtk-text-buffer-insert-markup
Inserts the text in markup at position iter . markup will be inserted in its entirety and must be nul-terminated and valid UTF-8. Emits the “insert-text” signal, possibly multiple times; insertion actually occurs in the default handler for the signal. iter will point to the end of the inserted text on return.
Upvotes: 0
Reputation: 3244
To specify that some text in the buffer should have specific formatting, you must define a tag to hold that formatting information, and then apply that tag to the region of text using create_tag("tag name", property)
and apply_tag(tag, start_iter, end_iter)
as in, for instance:
tag = textbuffer.create_tag("orange_bg", background="orange")
textbuffer.apply_tag(tag, start_iter, end_iter)
The following are some of the common styles applied to text:
You can also delete particular tags later using remove_tag()
or delete all tags in a given region by calling remove_all_tags()
.
Upvotes: 1