3

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()
TingPing
  • 2,129
  • 1
  • 12
  • 15
mortymacs
  • 3,456
  • 3
  • 27
  • 53
  • Gtk.Textview does not have a placeholder nor related methods – José Fonte Jun 23 '17 at 10:18
  • 1
    @JoséFonte yes, but i'm looking for a way to implement it. – mortymacs Jun 23 '17 at 10:21
  • 1
    The question is not so clear. Well, ain't easy to implement the behavior of the placeholder. Maybe by using different textbuffers and using signals to swap the buffers. – José Fonte Jun 23 '17 at 10:26
  • I have had the same use case. Not sure why the gtk-devs provided this by default for gtk-entry but not for textview. My use case is "exam question flip-cards" with one question on one side, you flip it, the answer is shown. I want to provide additional hints to the first-time user how to use it, so placeholder text would be useful. (I already also use tooltips and pop-over widgets, but users are different. Some need more help than others. I differ between first-time starting the widget, and continued use of course.) Would be convenient if more widgets in gtk3 could have that work as-is. – shevy Feb 09 '21 at 12:52

1 Answers1

2

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.

ptomato
  • 56,175
  • 13
  • 112
  • 165