Search code examples
python-3.xgtkpygobject

How to Add Placeholder on TextView Object in PyGObject


I've added placeholder on Entry object:

self._widget = Gtk.Entry()
self._widget.set_placeholder_text("Enter your name")

Now I want to add placeholder on TextView object, but I didn't find any method for it:

self._widget = Gtk.TextView()

Solution

  • You should connect to focus-in-event and focus-out-event. If the text buffer is empty on focus-out-event, set a placeholder flag to true, add the placeholder text, and add a CSS class that you can optionally use to style the placeholder text gray. If the placeholder flag is true on focus-in-event, empty the text buffer, and remove the CSS class.