2

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?

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.