Harsh J

Memoirs of a QWERTY Keyboard

Setting a custom label for a Button with stock icon in PyGTK

8 comments

This … might not be useful to all of you out there, but those new to PyGTK (Python binding for GTK) might find it so.

Ok, so you generally create a stock icon’ed Button like this (For OK, as an example):

Button=gtk.Button(stock="gtk-ok")

Now as you might have noticed, directly setting the label for a stock icon button doesn’t change it. So here’s an easy way to do that:

Label=Button.get_children()[0]
Label=Label.get_children()[0].get_children()[1]
Label=Label.set_label(“Custom Label”)

Thus the “Ok” string changes to “Custom Label” just like this:

I wish it could auto-set the label when called with the normal routine like below:

Button=gtk.Button(“Some Label”,stock=”gtk-some-stock-button”)

Credits to PyGTK Mailing List Archives.

Written by Harsh

November 17th, 2007 at 8:47 pm

Posted in Uncategorized

Tagged with

8 Responses to 'Setting a custom label for a Button with stock icon in PyGTK'

Subscribe to comments with RSS or TrackBack to 'Setting a custom label for a Button with stock icon in PyGTK'.

  1. Hi there,

    thanks for this small advice. It qas quite handy for me since I just had the exact problem of setting a button set by glade but leaving its icon. Just using set_label on the widget itself led to a button with no icon and settings reset.
    Your solution has pointed me to the solution that I recheck glade and I see the structure there that the “get_children”-stuff is all about. So now I only reference the label directly and everything is fine.

    Thanks, visit http://blueproximity.sf.net to see the project where this advice was used.

    Bye
    Lars

    Lars Friedrichs

    22 Nov 07 at 4:34 pm

  2. Glad it was of assistance to you :)

    I’ve actually used that Bluetooth Proximity thing once, while I was trying out all things possible using my Nokia 6681 + Ubuntu. Nice work indeed, keep it up! :D

    I’ve tried using Glade for my PyGTK applications but its just too confusing. I’ve used VB earlier while working on Windows and designing on it was sort of easier and smoother (Being able to place anywhere at will).

    But Glade is very irritating, or maybe am not adding elements into my forms the right way. Do you know of a good tutorial to start Glade with? Cause its painful manually coding in every window element. :(

    Harsh

    22 Nov 07 at 5:31 pm

  3. It’s funny how small the world seems to be. So you tried my program – may I ask where you downloaded it from? Just for my statistics :-)
    Actually I can’t remember exactly where I started at but I believe it was this one:
    http://www.learningpython.com/2006/05/07/creating-a-gui-using-pygtk-and-glade/
    Afterwards I mostly used the official docs and google for specific problems.

    Bye
    Lars

    Lars Friedrichs

    22 Nov 07 at 10:30 pm

  4. btw, I used VB (VBA within Access to be exact) earlier too. I believed it to be rather quick and convenient but I must admit that there are serious issues with that way of designing forms. Try resizing such a form – either it’s not possible or you need very dirty hacks and it’s slow.
    Having a layout manager like with gtk or java awt seems a bit strange in the beginning since one is confused where it puts things automatically but one is usually getting used to it fast and afterwards it is very rewarding. You get professional style guis and they are resizable by the user without any hassle.
    Layout managers are simply better than an absolute layout – I also learned that the hard way.

    Bye
    Lars

    Lars Friedrichs

    22 Nov 07 at 10:39 pm

  5. I was searching and I think I’d got it through a Softpedia result. This page. :)

    And thanks for that link, its well written. :]

    And yes, VB sure does have its flaws(Agree on the form resizing part, very true!), and its nowhere as powerful as Python either. I’ll try getting into using Glade this week, many thanks for the help and clarification!

    Harsh

    22 Nov 07 at 10:53 pm

  6. Hi,
    Thank you very much for this tip !
    I’ve spent a long time before finding such post.
    I needed it for the ImageMenuItem widget.
    For information, it works like this :

    menu=gtk.Menu()
    item=gtk.ImageMenuItem(gtk.STOCK_XXX)
    item.get_children()[0].set_label('My _MenuItem')
    menu.append(item)
    item.connect('activate', method)
    item.show()

    @+

    ekingr

    12 Dec 07 at 10:17 pm

  7. ekinger – Glad it was of help, and thanks for the Menu snippet as well! :)

    Harsh

    12 Dec 07 at 10:25 pm

  8. I believe the “canonical” way of doing this is to create new stock icons and icons sets, something like this:


    stock_items = (
    ("my-gtk-ok", "_Custom Label", 0, 0, None),
    ("my-gtk-cancel", "Custom _Label", 0, 0, None),
    )

    stock_aliases = (
    ("my-gtk-ok", gtk.STOCK_OK),
    ("my-gtk-cancel", gtk.STOCK_CANCEL),
    )

    gtk.stock_add(stock_items)

    factory = gtk.IconFactory()
    factory.add_default()

    style = gtk.Style()

    for item, alias in stock_aliases:
    icon_set = style.lookup_icon_set(alias)
    factory.add(item, icon_set)

    vbox = gtk.VBox()
    button = gtk.Button(stock="my-gtk-ok")
    vbox.pack_start(button)
    button = gtk.Button(stock="my-gtk-cancel")
    vbox.pack_start(button)

    ...

    Jordan Callicoat

    28 May 08 at 8:30 am

Leave a Reply