0

I am attaching a piece of my code in which I try to display the text in the Entry field with predefined text. I use entry.set_placeholder_text for this, but unfortunately the text does not appear. Is there an effective solution?

# -*- coding: utf-8 -*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class MyWindow(Gtk.Window):
    def __init__(self):
        super(MyWindow, self).__init__()
        entry = Gtk.Entry()
        entry.set_placeholder_text("Any text")
        self.add(entry)

win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
pppery
  • 3,731
  • 22
  • 33
  • 46
Radek
  • 57
  • 6

2 Answers2

1

The placeholder text is only visible when the entry is empty and unfocused. Adding another widget that can get the focus will make the text appear.

Example:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class MyWindow(Gtk.Window):
    def __init__(self):
        super(MyWindow, self).__init__()
        entry = Gtk.Entry()
        entry.set_placeholder_text("Any text")
        box = Gtk.HBox()
        self.add(box)
        button = Gtk.Button()
        box.pack_start(button, False, False, 0)
        box.pack_start(entry, True, True, 0)

win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
bohrax
  • 1,051
  • 8
  • 20
1

I know, I have no idea why it doesn't work. I usually do:

 search_entry.props.placeholder_text = 'Placeholder text'

before calling set_active on the widget

mijorus
  • 111
  • 1
  • 7