[pygtk] emulate Live Bookmarks buttons?

Karl Ostmo kostmo at gmail.com
Thu Sep 11 09:49:17 WST 2008


>
> You can manually tweak gtk.MenuToolButton to do what you want
>

Thanks, that works.  Next it would be great if I could subclass
gtk.MenuToolButton with said modifications for my widget library.  I'm
getting some unexpected warnings, however.  First, here is a working example
of the widget modified inline:

#!/usr/bin/python

from pygtk import require
require('2.0')
import gtk

class Playground(gtk.Window):
    def __init__(self):
        gtk.Window.__init__( self, gtk.WINDOW_TOPLEVEL )

        toolbar = gtk.Toolbar()
        self.add(toolbar)
        toolbar.set_size_request(200, -1)

        menu_button = gtk.MenuToolButton(None, None)
        menu = gtk.Menu()
        [menu.append(gtk.MenuItem(video_source)) for video_source in
["Peas", "Carrots"]]
        menu.show_all()
        menu_button.set_menu(menu)
        hbox = menu_button.get_child()
        button, toggle_button = hbox.get_children()
        hbox.remove(button)
        img = gtk.image_new_from_stock(gtk.STOCK_DIRECTORY,
gtk.ICON_SIZE_SMALL_TOOLBAR)
        arrow = toggle_button.get_child()
        toggle_button.remove(arrow)
        hbox = gtk.HBox()
        hbox.pack_start(img, False, False)
        hbox.pack_start(gtk.Label("Live Bookmarks"), False, False)
        hbox.pack_start(arrow, False, False)
        toggle_button.add(hbox)
        toolbar.insert(menu_button, -1)

        self.show_all()

if __name__ == "__main__":
    application = Playground()
    gtk.main()



However, when I attempt to perform the same modifications in the __init__ of
a subclass, it displays correctly but I get two warnings:

./classed_arrow_menubutton.py:38: GtkWarning: gtk_container_add: assertion
`GTK_IS_CONTAINER (container)' failed
  toolbar.insert(menu_button, -1)
./classed_arrow_menubutton.py:38: GtkWarning: gtk_button_set_relief:
assertion `GTK_IS_BUTTON (button)' failed
  toolbar.insert(menu_button, -1)

The code for my attempt is as follows.  Any idea how to create this subclass
properly?

Thanks,
Karl


#!/usr/bin/python

from pygtk import require
require('2.0')
import gtk

class MenuToolButtonSingle(gtk.MenuToolButton):
    def __init__(self, stock_id, label_text):
        gtk.MenuToolButton.__init__(self, None, None)

        hbox = self.get_child()
        button, toggle_button = hbox.get_children()
        hbox.remove(button)
        img = gtk.image_new_from_stock(stock_id,
gtk.ICON_SIZE_SMALL_TOOLBAR)
        arrow = toggle_button.get_child()
        toggle_button.remove(arrow)
        hbox = gtk.HBox()
        hbox.pack_start(img, False, False)
        hbox.pack_start(gtk.Label(label_text), False, False)
        hbox.pack_start(arrow, False, False)
        toggle_button.add(hbox)

class Playground(gtk.Window):
    def __init__(self):
        gtk.Window.__init__( self, gtk.WINDOW_TOPLEVEL )

        toolbar = gtk.Toolbar()
        self.add(toolbar)
        toolbar.set_size_request(200, -1)

        menu_button = MenuToolButtonSingle(gtk.STOCK_DIRECTORY, "Live
Bookmarks")

        menu = gtk.Menu()
        [menu.append(gtk.MenuItem(video_source)) for video_source in
["Peas", "Carrots"]]
        menu.show_all()
        menu_button.set_menu(menu)

        toolbar.insert(menu_button, -1)

        self.show_all()

if __name__ == "__main__":
    application = Playground()
    gtk.main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20080910/7bb72218/attachment-0001.htm 


More information about the pygtk mailing list