[pygtk] Subclassing gtk.ToolItem

zsolt foldvari zsolt.foldvari at gmail.com
Tue Apr 29 19:40:32 WST 2008


Gian Mario,

On 4/29/08, Gian Mario Tagliaretti <gianmt at gnome.org> wrote:
> On Tue, Apr 29, 2008 at 11:47 AM, zsolt foldvari
>  <zsolt.foldvari at gmail.com> wrote:
>  >  Yes, I did not modify the attached code. However, the behaviour is the
>  >  same independently whether the 'self' is included or not.
>  >  Actually, the code doesn't even get to the first line of the method
>  >  whatever that is.
>
>
> What versions of pygtk/pygobject are you using?

pygtk2-2.10.6-1.fc7
pygobject2-2.12.3-3.fc7
pygobject2-devel-2.12.3-3.fc7
pygtk2-codegen-2.10.6-1.fc7
pygobject2-doc-2.12.3-3.fc7
pygtk2-devel-2.10.6-1.fc7
pygtk2-libglade-2.10.6-1.fc7

>  Do you have a minimal but complete test case that we can just
>  copy/paste to test?

I have just quickly modified the uimanager.py coming in the
examples/gtk/ directory of pygtk (below).

Thanks a lot,
Zsolt


<code>

#
# Small example of the new GtkUIManager
#
# Johan Dahlin <johan at gnome.org>, 2004
#

import pygtk
pygtk.require('2.0')

import gtk

ui_string = """<ui>
  <menubar name='Menubar'>
    <menu action='FileMenu'>
      <menuitem action='New'/>
      <menuitem action='Open'/>
      <separator/>
      <menuitem action='Close'/>
      <menuitem action='Quit'/>
    </menu>
    <menu action='HelpMenu'>
      <menuitem action='About'/>
    </menu>
  </menubar>
  <toolbar name='Toolbar'>
    <toolitem action='New'/>
    <toolitem action='Open'/>
    <separator/>
    <toolitem action='Quit'/>
    <separator/>
    <toolitem action='comboaction'/>
  </toolbar>
</ui>"""

class Window(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_title('GtkUIManager test app')
        self.connect('delete-event', self.delete_event_cb)
        self.set_size_request(400, 200)
        vbox = gtk.VBox()
        self.add(vbox)

        self.create_ui()
        vbox.pack_start(self.ui.get_widget('/Menubar'), expand=False)
        vbox.pack_start(self.ui.get_widget('/Toolbar'), expand=False)

        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        vbox.pack_start(sw)

        textview = gtk.TextView()
        self.buffer = textview.get_buffer()
        sw.add(textview)

        status = gtk.Statusbar()
        vbox.pack_end(status, expand=False)

    def create_ui(self):
        ag = gtk.ActionGroup('WindowActions')
        actions = [
            ('FileMenu', None, '_File'),
            ('New',      gtk.STOCK_NEW, '_New', '<control>N',
             'Create a new file', self.file_new_cb),
            ('Open',     gtk.STOCK_OPEN, '_Open', '<control>O',
             'Open a file', self.file_open_cb),
            ('Close',    gtk.STOCK_CLOSE, '_Close', '<control>W',
             'Close the current window', self.file_close_cb),
            ('Quit',     gtk.STOCK_QUIT, '_Quit', '<control>Q',
             'Quit application', self.file_quit_cb),
            ('HelpMenu', None, '_Help'),
            ('About',    None, '_About', None, 'About application',
             self.help_about_cb),
         ]
        ag.add_actions(actions)

        comboaction = ComboToolAction('comboaction', 'Test', 'Test', None)
        ag.add_action(comboaction)

        self.ui = gtk.UIManager()
        self.ui.insert_action_group(ag, 0)
        self.ui.add_ui_from_string(ui_string)
        self.add_accel_group(self.ui.get_accel_group())

    def file_new_cb(self, action):
        w = Window()
        w.show_all()
        gtk.main()

    def file_open_cb(self, action):
        dialog = gtk.FileChooserDialog("Open..", self,
                                       gtk.FILE_CHOOSER_ACTION_OPEN,
                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                        gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        dialog.set_default_response(gtk.RESPONSE_OK)

        filter = gtk.FileFilter()
        filter.set_name("All files")
        filter.add_pattern("*")
        dialog.add_filter(filter)

        dialog.hide()

        if dialog.run() == gtk.RESPONSE_OK:
            filename = dialog.get_filename()
            self.buffer.set_text(file(filename).read())

        dialog.destroy()

    def file_close_cb(self, action):
        self.hide()
        gtk.main_quit()

    def file_quit_cb(self, action):
        raise SystemExit

    def help_about_cb(self, action):
        dialog = gtk.MessageDialog(self,
                                   (gtk.DIALOG_MODAL |
                                    gtk.DIALOG_DESTROY_WITH_PARENT),
                                   gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
                                   "Small example of the new GtkUIManger")
        dialog.run()
        dialog.destroy()

    def delete_event_cb(self, window, event):
        gtk.main_quit()

class ComboToolItem(gtk.ToolItem):

    __gtype_name__ = "ComboToolItem"

    def __init__(self):
        gtk.ToolItem.__init__(self)

        self.set_border_width(2)
        self.set_homogeneous(False)
        self.set_expand(False)

        self.combobox = gtk.combo_box_entry_new_text()
        for text in ['a', 'b', 'c', 'd', 'e', 'f']:
            self.combobox.append_text(text)
        self.combobox.show()
        self.add(self.combobox)

    def do_set_tooltip(self, tooltips, tip_text=None, tip_private=None):
        gtk.ToolItem.set_tooltip(self, tooltips, tip_text, tip_private)

        tooltips.set_tip(self.combobox, tip_text, tip_private)

class ComboToolAction(gtk.Action):

    __gtype_name__ = "ComboToolAction"

    def __init__(self, name, label, tooltip, stock_id):
        gtk.Action.__init__(self, name, label, tooltip, stock_id)

ComboToolAction.set_tool_item_type(ComboToolItem)


if __name__ == '__main__':
    w = Window()
    w.show_all()
    gtk.main()

</code>


More information about the pygtk mailing list