3

I'm trying to get buttons with image and label and didn't succed. I can have button with label OR image but not both. This is my part of code:

try:
    pb = Pixbuf.new_from_file_at_size('myimg.jpg', 100, 100)
except:
    pb = None
img = Gtk.Image()
img.set_from_pixbuf(pb)
button1 = Gtk.Button(xalign=0.5, yalign=1)
#button1.set_label(lbl)
button1.set_image(img)
button1.set_image_position(Gtk.PositionType.TOP)
button1.get_style_context().add_class("btn_article")

any idea ? Thanks

1 Answer 1

8

Doc: Gtk.Button.set_image (widget, data): "The image will be displayed if the label text is NULL". The image here usually is an icon, therefore there is a icon-name associate with it and (depends on settings) if necessary showing the label according the icon. Reverse the situation, the label is used to find an icon associate with it and (if necessary) showing the icon. Use:

button1.set_always_show_image (True)

or, if that doesn't succeed, create a container inside (in your case Gtk.Grid) and pack inside the button, i.e:

grid = Gtk.Grid ()
img = Gtk.Image()
img.set_from_pixbuf(pb)
label = Gtk.Label ('test')

grid.attach (img, 0, 0, 1, 1)
grid.attach (label, 0, 1, 1, 1)
grid.show_all ()    

button1.add (grid)
Sign up to request clarification or add additional context in comments.

2 Comments

I am using ruby but the python syntax works almost 1:1. The .set_always_show_image(true) seems to have worked just fine. I actually ended up using a horizontal box though - was easier to wrap my mind around it as opposed to a grid.
Nah in my case set_always_show_image will indeed hide the label again. So creating a container with a label + image would be the only way.

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.