1

I am trying to create a Button in Gtk4.0 and the button should have an Icon and a Label.
But when I set the Label, the image will be deleted, and when i set the image (via btn.set_icon_name) then the label will be deleted.

After a bit of research, i found out that this is how it is suposed to work : (developer-old.gnome.org)

Adds a GtkImage with the given icon name as a child. If button already contains a child widget, that child widget will be removed and replaced with the image.

I found this on Stackoverflow: Python GTK3 button with image and label, and I'm sure that it works fine in Gtk 3.0 but I am using Gtk 4.0

Where is the Gtk.Button.set_always_show_image(True) Method in Gtk 4.0?

Heschy
  • 31
  • 5

1 Answers1

2

There is no such method in GTK4, because buttons can have only three states:

  • an icon
  • a label
  • a custom widget

If you want to show both a label and an icon inside a GtkButton, then pack a label and an icon inside a GtkBox, and put the box inside the button.

As a UI definition:

<object class="GtkButton">
  <property name="child">
    <object class="GtkBox">
      <property name="spacing">6</spacing>
      <child>
        <object class="GtkImage">
          <property name="icon-name">some-icon</property>
        </object>
      </child>
      <child>
        <object class="GtkLabel">
          <property name="label">Some Text</property>
        </object>
      </child>
    </object>
  </property>
</object>

As Python code:

box = Gtk.Box(spacing=6)
icon = Gtk.Image(icon_name="some-icon")
label = Gtk.Label(label="Some Text")
box.append(icon)
box.append(label)
button = Gtk.Button(child=box)
ebassi
  • 8,648
  • 27
  • 29