1

I am trying to create a custom Gtk text entry. The basic idea is to put a button inside of a text entry. Here is a shortened version of my full code:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
builder = Gtk.Builder()
button = Gtk.Button('button')
entry = Gtk.Entry()
entry.add_child(builder, button, "button")

The button does not get shown and it fails with the error:

(pygtk_foobar.py:26622): Gtk-CRITICAL **: gtk_buildable_add_child: 
assertion 'iface->add_child != NULL' failed

Any suggestions?

theGtknerd
  • 3,647
  • 1
  • 13
  • 34

1 Answers1

2

A GtkEntry widget is not a GtkContainer, and thus it cannot have child widgets added or removed to it.

Only widgets that inherit from the GtkContainer class can have children in GTK+.

If you want to put a button next to a text entry, you should pack both widgets into a container like GtkBox, e.g.:

box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=2)
entry = Gtk.Entry()
button = Gtk.Button(label='button', hexpand=True)
box.add(entry)
box.add(button)
box.show_all()

This will create a box with an entry and a button.

ebassi
  • 8,648
  • 27
  • 29
  • I have tried this already and will use it as a last resort. What I do not like about it is the button appears **outside** the entry. I want the button inside the entry. Just like a spinbutton, but a spinbutton will not work since I need to enter custom formatted text. If, yes if, the input and output would work for spinbuttons in Python, my problem would be solved. I will keep on searching, I guess. – theGtknerd Apr 24 '17 at 21:34
  • A complement to @ebassi suggestion, would be to add the **linked** style class to the box, this would "merge" both widgets and give a sense of homogeneity, if we can call it that, to both widgets. Another workaround would be to use the primary and secondary "clickable" icons available on gtk entry. – José Fonte Apr 25 '17 at 00:41
  • Thanks everyone. I started playing with the combination of multiple Widgets in a GtkBox using CSS. I believe this will work perfectly since I can theoretically 'merge' multiple entries to appear as one entry while separating the text into chunks. By the by, I am creating a specialized datetime editing widget. I know, so foolish, however in this instance I want a nice display that does not let the user input wrong values. – theGtknerd Apr 25 '17 at 02:07
  • @JoséFonte. Would you mind elaborating what a linked style class is? – theGtknerd Apr 25 '17 at 02:08
  • It's a css class which does what you described in your previous comment. – José Fonte Apr 25 '17 at 09:03
  • 1
    Check linked buttons on [this link](https://wiki.gnome.org/HowDoI/Buttons). – José Fonte Apr 25 '17 at 09:08