Michael Schwartz
Michael Schwartz

Reputation: 8415

PYGTK Menubar Hotkeys Label

Does anyone know how to comment MenuItems in pygtk?

Menubar hotkey label example on Firefox

I'm trying to add ALT+F4 to the right of Exit.

Any help is greatly appreciated.

#!/usr/bin/env python

import gtk

class MenuBar(gtk.Window):
  def __init__(self):
   super(MenuBar, self).__init__()
   self.set_position(gtk.WIN_POS_CENTER)
   self.set_title("MenuBar")
   self.set_size_request(320, 75)
   self.set_keep_above(False)
   self.connect("destroy", gtk.main_quit, "WM destroy")

   mb = gtk.MenuBar()
   filemenu = gtk.Menu()
   filem = gtk.MenuItem("File")
   filem.set_submenu(filemenu)

   exit = gtk.MenuItem("Exit")
   exit.connect("activate", gtk.main_quit)
   filemenu.append(exit)
   mb.append(filem)

   vbox = gtk.VBox(False, 0)
   vbox.pack_start(mb, False, False, 0)
   self.add(vbox)

   label = gtk.Label('An easy, but simple menubar!')
   vbox.pack_start(label, False, False, 10)
   self.show_all()

MenuBar()
gtk.main()

Upvotes: 0

Views: 318

Answers (1)

jcoppens
jcoppens

Reputation: 5440

I've just hit the same problem (well, a couple of days ago). From what I learned, there is a new level of abstraction call GtkActionGroup.

So, the 'legacy' (or old :) way to do menus is to define them the way you did (and me too), by defining the items one by one and attaching handlers. This works fine till you hit the necessity for accelerators.

The 'new' way is to define all the actions you need in your program (independently of how you will activate them), grouped by related functions (hence 'group'ed), then connect each menuitem to its corresponding action. This is handy, after getting used to it, because it means you don't have to repeat all the work for a toolbar. You just define each toolbutton, and link it to the same action. And you need to define actions only once (instead of once as shortcut, once in the menu, once in the toolbar, and who knows where else).

I understand this is because gtk maintains a global accelerator map.

http://faq.pygtk.org/index.py?req=show&file=faq11.008.htp shows simply how to create actions. http://python-gtk-3-tutorial.readthedocs.org/en/latest/menus.html is a more complete (and complex) example of how to make menus.

Like gtktextviews and gtktreeviews, menus don't get simpler... But it is mostly one's resistance to change.

Upvotes: 2

Related Questions